# Sorting algorithm 05 ——– heap sorting (Graphic)

Time：2021-2-22

1. Heap sort

Heap sort is a sort algorithm designed with heap data structure. It is similar to a binary tree and has a characteristic that the value of parent node is greater than (less than) the value of child node.

There are two kinds of heaps. The one whose parent node is larger than the child node is called the maximum heap, and the one whose parent node is smaller than the child node is called the minimum heap

Here’s the largest heap 2. Heap sorting steps

Let’s take the largest heap as an example. Suppose there are n elements,

1) Construct maximum heap

2) Exchange the values of the root node and the nth node

3) Adjusts the current heap to the maximum heap

4) N minus one, continue step 2) 3) until n = = 1

3. How to construct the maximum heap

According to the nature of maximum heap, the value of each parent node is larger than that of the child node, so it needs to be adjusted from the bottom to the top. After adjustment, the root node is the largest. for instance

Suppose array: 1) Think of it as the following form (complete binary tree), which is sorted in order in the array, 2) Start building maximum heap

1. Find the last parent node first. Because the maximum heap is in the form above, it’s easy to get the subscript of the last parent node. The above elements are 8

Then the subscript of the last parent node: I = 8 / 2-1 = 3, and the subscript of the left child node of the node is: 2I + 1, and the subscript of the right child node is: 2I + 2. This is a very important property.

Compare the corresponding values of I and 2I + 1, 2I + 2. If the two child nodes are larger than the parent node, then exchange the value of the parent-child node. Pay attention to judge whether 2I + 1 and 2I + 2 exist (that is, the subscript cannot exceed the boundary) The second time: third time The fourth time At this time, it should be noted that after the exchange, the following nodes may not meet the nature of the maximum heap. At this time, continue to follow the above steps, which is actually a recursion. It’s easy to understand when you look at the code

The fifth time, the adjustment is completed Start the exchange

3) Exchange the value of the root node and the nth-i (I is the number of exchanged elements) 4) Then adjust it to a maximum heap, and adjust it from top to bottom, that is, from the root node. 5) Continue with step 3), 4) until n-i = = 1, and the sorting is completed

It’s easy to understand through the code

The code is as follows:

``````#include
//Exchange the value of two numbers
void swap(int *a,int * b)
{
int temp=*a;
*a=*b;
*b=temp;
}
//Constructing maximum heap process
{
Int parentnode = start; // parent node
Int childnode = parentnode * 2 + 1; // left child node
while(childNode<=end)
{
//Childnode + 1 is the right child node
if(childNode+1<=end&&arr[childNode+1]>arr[childNode])
Childnode + +; // if the right child node is large, take the right child node
//The parent node is larger than the child node, so it does not need to exchange and returns directly
if(arr[parentNode]>arr[childNode])
return;
//Otherwise, the values of the parent and child nodes are exchanged
else
{
swap(&arr[parentNode],&arr[childNode]);

//After switching, it may lead to the following nodes
//Does not meet the requirements of the maximum heap, so continue to construct the following nodes
parentNode=childNode;
childNode=parentNode*2+1;
}
}

}
{
//Num number of array elements
int i;
//Start by building a maximum heap
for(i=num/2-1;i>=0;i--)

for(i=num-1;i>0;i--)
{
Swap (& arr [i], & arr ); // swap the first element with the I (I starting from the back)
Maxhead (arr, 0, i-1); // continue to construct the maximum heap after exchange
}
}
int main()
{
int i;
int arr={1,5,0,6,3,9,8,7};
Headsort (arr, 8); // heap sort
for(i=0;i<8;i++)
printf("%d\n",arr[i]);
return 0;
}``````

The average time complexity of heap sorting is: O (nlogn)