6 heap sort

Heap sort is a kind of tree selection sort that uses the nature of the heap. It belongs to the original order.

 

(1) The nature of the heap:

Heap has two properties, namely structural properties and heap order properties.

Structural properties: Heap is a complete binary tree.

The nature of heap order (big top heap): For any node x, the key value in x is greater than or equal to the key value in the left subtree node or the right subtree node.

 

(2) Ideas:

     1) Construct the initial key sequence to be sorted (R1, R2...Rn) into a large top pile, and the top element R[1] is the largest element at this time.

     2) Exchange the top element R[1] with the last element R[n], and get a new disordered area (R1, R2,...Rn-1) and a new ordered area ( Rn), and satisfy R[1,2...n-1]<=R[n].

     3) Since the new top of the heap R[1] after the exchange may violate the nature of the heap, the disordered area (R1, R2,...Rn-1) needs to be adjusted to the new heap, and then R[ 1] Exchange with the last element Rn-1 of the disordered region to obtain a new disordered region (R1, R2...Rn-2) and a new ordered region (Rn-1, Rn). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is completed.

      Therefore, for heap sorting, the two most important operations are to construct the initial heap and adjust the heap. In fact, the construction of the initial heap is actually the process of adjusting the heap, but the construction of the initial heap is to adjust all non-leaf nodes.

 

(3) Complexity analysis:

(3.1) Time complexity:   

         O (nlogn)。

(3.2) Space complexity:

         O (1)。

 

(4) Stability:

         Heap sorting is not a stable sorting algorithm.

 

(5) Performance summary:

Heap sort is suitable for occasions where the amount of data is very large (millions of data).

Heap sort does not require a large number of recursive or multi-dimensional temporary storage arrays. This is suitable for sequences with very large amounts of data. For example, for more than millions of records, because quick sort and merge sort use recursion to design algorithms, stack overflow errors may occur when the amount of data is very large.

Heap sorting will build all the data into a heap, with the largest data at the top of the heap, and then exchange the top data with the last data in the sequence. Next, rebuild the heap again, exchange data, and continue in order to sort all the data.

 

code segment:

#include <iostream>
#include<algorithm>
using namespace std;
#define SIZE 6

void heap_sort(int* a,int size);
void build_heap(int* a,int size);
void heap_adjust(int* a,int i,int size);

int main(int argc, char *argv[])
{
    int a[]={0,16,7,3,20,17,8};		//注意a[]有效元素从a[1]开始!!!
	cout<<"排序前: "<<endl;
    for(int i=1;i<=SIZE;i++)
	{
		cout<<a[i]<<" ";
	}
    cout<<endl;
	cout<<"-------------------------------------"<<endl;

    heap_sort(a,SIZE);
	cout<<"-------------------------------------"<<endl;
	cout<<"排序后: "<<endl;
    for(int i=1;i<=SIZE;i++)
	{
		cout<<a[i]<<" ";
	}
    cout<<endl;
    
    return 0;
}

//heap_sort():堆排序
/*
@param:
a:待排序数组
size:待排序数组长度
@ret:无
*/
void heap_sort(int* a,int size)
{
	build_heap(a,size);			//建堆
	for(int i=size;i>=1;i--)
	{
		swap(a[i],a[1]);		//交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到最后面
		heap_adjust(a,1,i-1);	//重新调整堆顶节点成为大顶堆
	}
}

//build_heap():构造初始堆
/*
@param:
a:堆元素
size:堆元素长度
@ret:
*/
void build_heap(int* a,int size)
{
	//构造初始堆,从最后一个非叶节点开始调整
	for(int i=size/2;i>=1;i--)		//非叶节点最大序号值为size/2 	
	{
		heap_adjust(a,i,size);
	}
}

//heap_adjust():调整堆(从第i节点到第size节点调整堆,实际上是调整以i节点为父节点的子树为堆)
/*
@param:
a:堆元素
i:堆第i节点
size:堆第size节点
@ret:无
*/
void heap_adjust(int* a,int i,int size)
{
	int lchild=2*i;			//i的左孩子节点序号
	int rchild=2*i+1;		//i的右孩子节点序号
	int max=i;

	if(i<=size/2)			//i是非叶节点时进行调整
	{
		if( lchild<=size&&a[lchild]>a[max] )
		{
			max=lchild;
		}
		if( rchild<=size&&a[rchild]>a[max] )
		{
			max=rchild;
		}
		if(max!=i)
		{
			swap(a[max],a[i]);
			heap_adjust(a,max,size);		//避免调整之后以max为父节点的子树不是堆
		}
	}
}

 

Guess you like

Origin blog.csdn.net/u012906122/article/details/103595499