[Eight classic sorting algorithms] Heap sort


I. Overview

Heap sort was proposed by JWJ Williams in 1964. He proposed an algorithm that uses the data structure of the heap for sorting, and called it heap sort. Heap sort is an improvement based on selection sort by maintaining a heap to select the largest (or smallest) element and placing it at the end of the array, and then recursively calling heap sort on the remaining elements.

Heap sort had some performance problems in its early versions. For example, the heap structure needed to be frequently adjusted during the heap construction process, resulting in performance degradation. In order to improve this problem, people have proposed an operation called "heap adjustment", which optimizes the process of adjusting the heap into a single pass, thus improving performance. In addition, there are some other improvement methods, such as using a binary heap instead of a normal heap, using a bottom-up method of building a heap, etc.

As a classic sorting algorithm, heap sort has become an efficient and stable sorting algorithm after years of development and improvement, and has been widely used in practical applications.


2. Interpretation of ideas

We know that heap sorting is a sorting algorithm based on heap data structure, so heap sorting is divided into the following steps:

①:Build a big pile (or a small pile). Here we start from the parent node of the last data in the array and use the downward adjustment algorithm to build the heap.
The downward adjustment algorithm has a premise: the left and right subtrees must be a heap before they can be adjusted. Also pay attention to whether to adjust the pile to a large or small pile.
Adjust the heap to a smaller (larger) size: Compare the top element of the heap with the smallest (largest) node among the children. If the parent node is greater (less than) the smaller child node, the two are exchanged. Keep adjusting downward to the appropriate position. (Turn up the pile to compare with older children)
Insert image description here

②: Exchange the largest (or smallest) element in the heap, that is, the top element of the heap, with the last element in the array, and then reduce the size of the heap by 1. Adjust the exchanged top element of the heap downward until the heap meets the heap properties again.
Insert image description here

③: Repeat the above steps until the size of the heap is 1, then the entire array is in order.


3. Code implementation (Dadai as an example)

void AdjustDown(int* a, int n, int parent)
{
    
    
	//建大堆
	int child = parent * 2 + 1;

	while (child < n)
	{
    
    
		if (child + 1 < n && a[child + 1] > a[child])
		{
    
    
			child++;
		}

		if (a[parent] < a[child])
		{
    
    
			Swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
    
    
			break;
		}
	}
}

//堆排序
void HeapSort(int* a, int n)
{
    
    
	//升序,建大堆
	for (int i = (n - 2) / 2; i >= 0; i--)
	{
    
    
		AdjustDown(a, n, i);
	}

	int end = n - 1;
	while (end > 0)
	{
    
    
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}
}

Time complexity: O(N*logN)
Space complexity: O(1)

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/132919814