Heapsort Profile

Heapsort

Heap sort in my opinion is a difficult sort algorithm, which tracks about it.

First, briefly explain the heap this structure.

Heap sort, by definition, it is the use of the heap of the data structure of the. Heap is a special kind of binary tree , where each element conform to certain rules.

8HsawQ.jpg

The maximum heap: Every parent, two child nodes than big. The minimum heap is smaller than the parent child nodes.

Arrays are of course was a sequence table, not such a configuration, the maximum stack now an example, each point on the map code on the number. Numbered from top to bottom, left to right.

8Hs0Fs.jpg

A property difficult to see the maximum stack: each node by the subscript 2, 2 + 1 by the obtained index is the node index of the two child nodes

Then you can put the biggest heap of nature manifested itself in mathematical form. For each node the maximum heap, has \ (arr [i]> = arr [i * 2] \ & \ & arr [i]> = arr [i * 2 + 1] \)

From now simulate what the operating principle of the heap sort , which also need some other functions, say the following.

Now there is a group of elements have been arranged ready by the largest heap of ways, from the last node to loop up until only a node unsorted.

For each node, the current node will exchange with the top element in the stack. Because the top of the heap element is the largest element of this stack , so we found the position of the maximum, and it came in the value of unsorted last position of the sequence, that is to say a good row data.

However, after the heap after the exchange, the largest heap of conditions have been satisfied, we then Adjust_Heap a function to be restored to its state of maximum heap. Next operates until the end of the cycle. In fact HEAPSORT main function is quite good writing.

void HeapSort(int *arr,int length)
{
	for (int i=length-1;i>1;i--)
	{
		swap(arr[i],arr[1]);
		HeapAdjust(arr,1,i-1);
	}
	/*for (int i=0;i<length;i++)//前移后面有解释
		arr[i]=arr[i+1];*/
}

But there are two problems.

  1. How to achieve Adjust_Heap?
  2. How to put a maximum heap does not meet the array into a maximum heap?

The second problem is to rely on the first question Adjust_Heap function to achieve, first look at the principle of Adjust_Heap.

Adjust_Heap as described above, is to restore the maximum heap. A little more specific, this function needs to know the current location does not comply with the maximum heap elements, a range of data recovery . (The latter is obvious, it can not have been lined up as a digital sequence of unsorted it back into the original position)

The operating mechanism function is to select from the given parent node starting from the two children in one of the largest and compare parent and child nodes larger, if the parent node is the largest, description has been processed; otherwise, this exchange two nodes, continue downward comparison.

void HeapAdjust(int *arr,int top,int range)//arr为数组首地址,top为需要进行恢复的元素下标,range就是范围啦
{
	for (int i=top*2;i<=range;i*=2)//为什么是*2?因为要获得子节点的下标
	{
		if (i<range&&arr[i]<arr[i+1])
			i++;//找到两个子节点中最大的元素的下标
		if (arr[top]>=arr[i])
			break;//不需要交换就退出
		swap(arr[top],arr[i]);//父子节点交换
		top=i;//现在要处理的点就是子节点
	}
}

The remaining question is only one, and how to transform a general array is the maximum heap structure? Here we use Initialize_Heap function to achieve. Implementation is very simple and crude, half of all data Adjust again on the line.

Of course, still have to explain it again for half of the data to be adjusted. Said data index * 2 above can be obtained under the subscript of its child nodes, in turn, only the index data * 2 or less total data number of the time will have child nodes thus has child nodes from traversal process again like the course all over again no problem.

void InitHeap (int *arr,int length)
{
	/*for (int i=length;i>=1;i--)
		arr[i]=arr[i-1];//数组是从0开始的,后移一次方便处理。别忘了最后换回来*/
	for (int i=length/2;i>0;i--)
		HeapAdjust(arr,i,length);//暴力过一遍
}

Heapsort to finish it ~

Guess you like

Origin www.cnblogs.com/Salty-Fish/p/12555987.html