[Sorting] Selection sorting and heap sorting (detailed illustration)

introduction

Starting from this article, several sorting algorithms will be introduced: selection sort, heap sort, direct insertion sort, Hill sort, bubble sort, quick sort, merge sort, and counting sort.

What I want to introduce in this article is selection sort and heap sort, both of which belong to selection sort.
The idea of ​​these two sorting algorithms is to select the largest or smallest element from the data elements to be sorted, and place it at the beginning or end of the sequence, so as to make the entire sequence orderly:

selection sort

train of thought

Selection sort fits the above description very well: select the smallest element from the array and place it at the beginning of the array. Then the element corresponding to the starting position is the correct position, which means that the part that needs to be sorted is reduced by one element. This loop can sort the entire array:

accomplish

This kind of thinking obviously requires two layers of loops:
the inner loop needs to determine the smallest element in the current part to be sorted, and record its subscript. After each inner loop, exchange the smallest element with the starting element of the part to be sorted; the
outer loop needs to reduce the size of the part to be sorted. Starting from the size of the array until the part to be sorted is 0, that is, the entire array is sorted:

insert image description here

void Swap(int* a, int m, int n)//交换
{
    
    
	int temp = a[n];
	a[n] = a[m];
	a[m] = temp;
}
//选择排序
void SelectSort(int* a, int n)
{
    
    
	for (int i = 0; i < n - 1; i++)
	{
    
    
		int min = a[i];
		int mini = i;
		for (int j = i + 1; j < n; j++)
		{
    
    
			if (a[j] < min)
			{
    
    
				min = a[j];
				mini = j;
			}
		}
		Swap(a, i, mini);
	}
}

heap sort

train of thought

Heap sorting is a sorting method based on binary trees. We introduced the data structures of binary trees and heaps in detail earlier.
For large heaps, the element at the top of the heap is the largest element in the heap. We only need to keep building the heap, and keep putting the elements at the top of the heap at the end of the current part to be sorted. It is equivalent to the end element is in its correct position. Gradually shrinking the size of the heap allows sorting the entire array.

accomplish

Under this idea, we need two loops:

The first loop implements building a heap of all elements of the current array.
When building a heap, there are two ways: upward adjustment and downward adjustment to build a heap:
when adjusting upward to build a heap, it is similar to the tail insertion of a binary tree, and then adjust the number of tail insertions upwards to a suitable position;
downward When adjusting the heap, that is to adjust each root node downwards and place it in its proper position. This requires that the subtrees of the root are all correct large heaps, so we need to adjust downwards from the root node of the penultimate layer:
(The time complexity of adjusting the heap downwards is less than that of upward adjustments, here Without proof, in this article, we only implement downward adjustment to build the heap) The
insert image description here
second cycle needs to realize the continuous exchange of the elements at the top of the heap with the elements at the end of the heap currently to be sorted, so that the elements can be sorted. Then the part to be sorted shrinks, and the part to be sorted builds a large pile again. until the end of the sort:

(The AdjustDown function for downward adjustment is explained in detail in the heap, so I won’t go into details here: poke me to see the heap and its implementation details )
insert image description here
insert image description here
insert image description here
insert image description here

void Swap(int* a, int m, int n)//交换
{
    
    
	int temp = a[n];
	a[n] = a[m];
	a[m] = temp;
}
void AdjustDwon(int* a, int n, int root)//向下调整建堆
{
    
    
	int parent = root;
	int child = parent * 2 + 1;
	while (child < n)
	{
    
    
		if (child + 1 < n && a[child + 1] > a[child])
		{
    
    
			child++;
		}
		if (a[child] > a[parent])
		{
    
    
			Swap(a, child, parent);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}
//堆排序
void HeapSort(int* a, int n)
{
    
    
	for (int i = (n - 2) / 2; i >= 0; i--)//建堆
	{
    
    
		AdjustDwon(a, n, i);
	}
	for (int i = 1; i < n ; i++)//排序
	{
    
    
		Swap(a, 0, n-i);
		AdjustDwon(a, n - i, 0);
	}
}

Summarize

So far, the content about selection sort and heap sort has been introduced.
Next, we will continue to introduce other sorts. Welcome to continue to pay attention.

If you think that I did not introduce a certain part clearly or that there is a problem with a certain part, you are welcome to raise it in the comment area

If this article is helpful to you, I hope it will be connected with one click

Hope to make progress together with you

Guess you like

Origin blog.csdn.net/weixin_73450183/article/details/130164008