[Data Structure — Sorting — Selection Sort]

1. Selection sorting

1.Basic idea

Each time, the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are arranged.

2. Directly select and sort

2.1 Algorithm explanation

• Select the data element with the largest (smallest) key in the element set array[i]–array[n-1]
• If it is not the last element in this set of elements one (first) element, then exchange it with the last (first) element in this set of elements
• In the remaining array[i]–array[n-2] (array[i+1]–array[n-1]) set, repeat the above steps until there is 1 element left in the set

Insert image description here

Summary of features of direct selection sort:

  1. Direct selection sort thinking is very easy to understand, but the efficiency is not very good. Rarely used in practice
  2. Time complexity: O(N^2)
  3. Space complexity: O(1)
  4. Stability: Unstable

2.2. Code implementation

2.2.1. Function definition
Sort.h
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>


//打印
void PrintArray(int* a, int n);
//选择排序
void SelectSort(int* a, int n);
2.2.2. Algorithm interface implementation
Sort.c
#include"Sort.h"


void PrintArray(int* a, int n)
{
    
    
	for (int i = 0; i < n; i++)
	{
    
    
		printf("%d ", a[i]);
	}
	printf("\n");
}

void Swap(int* p1, int* p2)
{
    
    
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

//选择排序
void SelectSort(int* a, int n)
{
    
    
	int begin = 0, end = n - 1;
	while (begin < end)
	{
    
    
		int mini = begin, maxi = begin;
		for (int i = begin; i <= end; i++)
		{
    
    
			if (a[i] > a[maxi])
				maxi = i;
			if (a[i] < a[mini])
				mini = i;
		}
		Swap(&a[begin], &a[mini]);
		if (begin == maxi)
			maxi = mini;
		Swap(&a[end], &a[maxi]);
		begin++;
		--end;
	}
}
2.2.3. Test code implementation
test.c
#include"Sort.h"

void TestSelectSort()
{
    
    
	int a[] = {
    
     2,4,5,7,8,0,9,6,3,1 };
	printf("排序前:");
	PrintArray(a, sizeof(a) / sizeof(int));
	printf("\n");
	printf("选择排序:");
	SelectSort(a, sizeof(a) / sizeof(int));
	PrintArray(a, sizeof(a) / sizeof(int));
}

int main()
{
    
    
    TestSelectSort();
    return 0;
2.2.4.Test display

Insert image description here

3. Heap sort

3.1. Algorithm explanation

Heapsort refers to a sorting algorithm designed using a data structure such as a stacked tree (heap). It is a type of selection sort. It selects data through the heap. It should be noted that if you sort in ascending order, you need to build a large heap, and if you sort in descending order, you need to build a small heap

If you want to learn more about heap sorting, you can go to my previous blogData structure - heap implementation (sequence table) (click to jump) a>, learn more about the implementation details. Here, we will simply explain and show the implementation code of heap sorting.

Insert image description here

3.2. Code implementation

3.2.1. Function definition
Sort.h
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>


//打印
void PrintArray(int* a, int n);
//堆排序
void HeapSort(int* a, int n);
3.2.2. Algorithm interface implementation
Sort.c
#include"Sort.c"

void Swap(int* p1, int* p2)
{
    
    
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

//向下调整建堆
void AdJustDown(int* a, int size, int parent)
{
    
    
	int child = parent * 2 + 1;
	while (child < size)
	{
    
    
		if (child + 1 < size && a[child + 1] > a[child])
		{
    
    
			child++;
		}
		if (a[child] > a[parent])
		{
    
    
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}
//堆排序
void HeapSort(int* a, int n)
{
    
    
	int end = n - 1;
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
    
    
		AdJustDown(a,n, i);
	}
	while(end > 0)
	{
    
    
		Swap(&a[0], &a[end]);
		AdJustDown(a, end, 0);
		end--;
	}
}
3.2.3. Test code implementation
test.c
#include"Sort.c"


void TestHeapSort()
{
    
    
	int a[] = {
    
     2,4,5,7,8,0,9,6,3,1 };
	printf("排序前:");
	PrintArray(a, sizeof(a) / sizeof(int));
	printf("\n");
	printf("堆排序:");
	HeapSort(a, sizeof(a) / sizeof(int));
	PrintArray(a, sizeof(a) / sizeof(int));
}
int main()
{
    
    
   TestHeapSort();
   
   return 0;
}
3.2.4.Test display

Insert image description here

Guess you like

Origin blog.csdn.net/qq_73900397/article/details/134911240