Sort - Sort heap

First, the algorithm description

  • Heap: heap is a data structure, the stack can be regarded as a complete binary tree, binary tree fully satisfied: value of any non-leaf node is not greater than (or less than) the value of its left and right child nodes. If the big father with small children, such a heap heap called the big top; if the big father a child, such a small heap called top heap.
  • Stack of: the current node (assumed to be a) the value of its child nodes are compared, and if the value is greater than a child node exists, then selecting from a exchange a maximum, when a next layer to when the above process is repeated until the value of a child node are smaller than a value of up to.

Second, the idea of ​​the algorithm

Heap sort execution process description (large top stack example) as follows:

  1. Construction stack: starting with the first non-leaf nodes of the binary random sequence is completely determined, from right to left, from bottom to top, for each node of the heap.
  2. Sort: After the end of the heap was built, top of the heap element is the largest element, and we will be the last element to be exchanged, the largest element that would put the subscript n position, at this time, to reduce a chaotic sequence of keywords, increase an ordered sequence of keywords. We then again in front of the n-1 elements of the stack, and then into the top of the stack elements the subscript n-1 position. Repeat this process until the remaining element of the heap, sorting is complete.

Third, the algorithm process

Now completed six integers {6,5, 14,7, 8, 1} big top heapsort

(1) build heap

  1. Adjustment 14, 14> 1, satisfying the definition of the stack, no adjustment.
  2. Adjustment 5,5 <7,5 <8, the stack does not satisfy the definition, so that the exchange 5 and 8, after an exchange of the leaf node 5 becomes, so the adjustment is completed.
  3. Adjustment 6,6 <8,6 <14, does not satisfy the definition of the stack, so the exchange 6 and 14. Continue to adjust 6,6> 1, satisfying the definition of the stack, no adjustment
    Here Insert Picture Description

(2) Sort 1

  1. The top of the stack 14 with the last exchange element 1, to reduce an unordered sequence of keywords, the keyword adding one ordered sequence.
  2. Adjustment 1,1 <8,1 <6, 1 and 8 exchange. Continue to adjust 1,1> 5,1> 7, 7 to exchange 1 and, after an exchange become leaf nodes, so the adjustment is
    Here Insert Picture Description

(3) Sort 2

Here Insert Picture Description

(4) Sort 3

Here Insert Picture Description

(5) Sort 4

Here Insert Picture Description

(6) sorting 5

Here Insert Picture Description


Fourth, Heap Sort insertion deletion

(1) the insertion node

需要在插入结点后保持堆的性质,因此需要先将要插入的结点x放在最底层的最右边,插入后满足完全二叉树的特点;然后把 x 依次向上调整到合适位置以满足堆的性质。

(2)删除结点

当删除堆的一个结点时,原来的位置就会出现一个空缺,填充这个空缺的方法就是,把最底层最右边的叶子的值赋给该空缺并下调到合适位置,最后把该叶子删除。


五、实现

void translate(int R[],int low,int high)	//	在数组R[low]到R[high]的范围内对在位置low的结点进行调整
{
	int i=low;	//	父结点
	int j=i*2;	//	左孩子
	int temp=R[i];	//	记录父结点
	while(j<=high)
	{
		if(j<high && R[j]<R[j+1])	//	若右孩子比较大,则把j指向右孩子
			j++;
		if(temp<R[j])
		{
			R[i]=R[j];	//	将R[j]调整到父结点的位置
			i=j;	//	修改i和j的值,以便继续向下调整
			j=2*i;
		}
		else
			break;	//	父结点较大,调整结束
	}
	R[i]=temp;	//	将被调整结点的值放入最终位置
}
void heapSort(int R[],int n)
{
	int i;
	int temp;
	for(i=n/2;i>=1;i--)	//	建立初始堆
	{
		translate(R,i,n);
	}
	for(i=n;i>=2;i--)
	{
		temp=R[i];	//	无序序列的堆顶和最后一个元素交换
		R[i]=R[1];
		R[1]=R[i];
		translate(R,1,i-1);
	}
}

六、算法性能分析

(1)时间复杂度分析

对于函数 translate(),显然 j 走了一条从当前结点到叶子结点的路径,完全二叉树的高度为 floor(log(n+1)),即对每一个结点调整的时间复杂度为 O(log n)

对于函数 heapSort(),基本操作总次数应该是两个并列的 for 循环中的基本操作次数之和,第一个循环的基本操作次数为 O(log n)*(n/2),第二个循环的基本操作次数为 O(log n)*(n-1),因此整个算法的基本操作次数为 O(log n)*(n/2) + O(log n)*(n-1),故其平均时间复杂度、最坏时间复杂度、最好时间复杂度均为 O(nlog n)

堆排序和快速排序的最好时间复杂度都是 O(nlog n),那么两者之间有什么不同呢?

  1. 10w 数据量两种排序速度基本相当,但是堆排序交换次数明显多于快速排序;10w+数据,随着数据量的增加快速排序效率要高的多,数据交换次数快速排序相比堆排序少的多。

  2. 实际应用中,堆排序的时间复杂度要比快速排序稳定,快速排序的最差的时间复杂度是O(n^2),平均时间复杂度是O(nlog n)。堆排序的时间复杂度稳定在O(nlog n)。但是从综合性能来看,快速排序性能更好。

(2)空间复杂度分析

空间复杂度为 O(1)

(3)稳定性

We know that node i is the stack structure of child nodes 2i and 2i + 1, the top of the stack requires a large parent node child nodes is greater than equal to 2, the top of the heap of small claim 2 which is less than or equal parent node child nodes. In a sequence of length n, heap sort process and the beginning of 3 the maximum value of its child nodes selected (large stack top) from the n / 2 or a minimum (minor vertex stack), the choice between these three elements Of course not undermine stability. But the process of building the stack from right to left, select the non-leaf node from bottom to top, when the non-leaf nodes is n / 2-1, n / 2-2, ... a parent node selecting these elements, it will destabilizing. Possible of the n / 2 behind the parent nodes exchange a first switching element has passed, and the first n / 2-1 the parent of a node does not exchange element behind the same, then the stability between the two identical elements it was destroyed. So, heap sort is not a stable sorting algorithm.

Guess you like

Origin blog.csdn.net/starter_____/article/details/94193977