"Introduction to Algorithms" heap sort C + + implementation (based on the algorithm introduction)

Source: Internet
Author: User

Before looking at other algorithms and data structure of the book to find the heap sorting code more difficult to write, read the "Introduction to the algorithm" found that is not so difficult, so I started to write a. The principle is based on an introduction to the algorithm of the third version of the heap sorting pseudo code.

 #include <iostream> using namespace std; const int array_size=10; int a[array_size]={

4,1,3,2,16,9,10,14,8,7}; Adjust POS position so that POS and its immediate children meet large heap conditions void max_heapify (int a[],int pos,int heap_size) {int l=2*pos+1,r=2*pos+2;//because subscript starts at 0,
	So here L and r add 1 int largest;
	if (L<=heap_size&&a[l]>a[pos]) largest=l;
	else Largest=pos;
	if (R<=heap_size&&a[r]>a[largest]) largest=r;
		if (largest!=pos) {int tmp=a[pos];
		A[pos]=a[largest];
		a[largest]=tmp; Max_heapify (a,largest,heap_size);//Changes the value of the largest position, the subtree with largest root also adjusts}//Build heap procedure void build_max_heap (int a[],int n) {fo R (int i=n/2-1;i>=0;i--)//Note the array subscript starts at 0, so here n/2-1 {max_heapify (a,i,n-1);//heap_size=n-1}} int main () {Build_max_h
	
	EAP (A,ARRAY_SIZE);
		for (int i=array_size-1;i>0;i--) {int tmp=a[0];//requires only one extra space for TMP, so the space complexity is O (1) a[0]=a[i];
		a[i]=tmp;
	Max_heapify (a,0,i-1);

	for (int i=0;i<array_size;i++) cout<<a[i]<<endl;
	System ("pause");
return 0; }

The heap sort time complexity is O (NLGN), because in Main, for O (n), each max_heapify adjustment heap is likely to recursively heap depth O (LGN), so the total complexity is O (NLGN).

In fact, initially see Build_max_heap build heap also want O (NLGN), because is also the cycle n times, each LGN, but because for different nodes, his height is not the same, then his adjustment max_heapify time is not the same, so accurate calculation can be seen build_max_ Heap's complexity is only O (n), please see "Introduction to Algorithms"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.