"Go" heap sort

Source: Internet
Author: User

Legend Reference http://www.cnblogs.com/mengdd/archive/2012/11/30/2796845.html

Original reference http://blog.csdn.net/eseaqyq/article/details/7497575

The following is an adjustment for the heap
void Heapajust (int data[],int i,int length)
{
int nchild;
int ntemp;
for (Ntemp=data[i];2*i+1<length;i=nchild)
{
nchild=2*i+1;
if (Nchild<length-1&&data[nchild+1]>data[nchild])//compare which child is bigger than oneself, if is right child, will nchild++;
{
nchild++;
}

if (Ntemp<data[nchild])//If it is smaller than your own largest child, swap
{
Data[i]=data[nchild];
Data[nchild]=ntemp;
}
else//if it's bigger than the biggest kid, it doesn't trade.
Break
}
}

Heap Sort
void heapsort (int data[],int length)
{
for (int i= (length>>1) -1;i>=0;i--)//Note this place: i= (length>>1)-1, plus parentheses, Reason: priority issue
{
Heapajust (data,i,length);//Initialize a heap
}
for (int j=length-1;j>0;--j)
{
int TEMP=DATA[J];
DATA[J]=DATA[0];
Data[0]=temp;
Heapajust (DATA,0,J);
}
}

The above is mainly functional functions, the main function of everyone to take care of! I've debugged it through already!
The time complexity of heap sequencing is O (NLOGN), and the worst case is the time complexity, which is O (1). But the heap sort is not stable!
In the interview process, a lot of time will be used to sort the heap, such as the following topics are the typical problem of heap sorting:

1. Give you 100w data to find the maximum of 10 elements. This time we can use the small top heap! What is this for?

2. Give you 100w data to find the minimum 10 elements. This time we can use the Big top heap! What is this for?

I believe there will be a lot of students will ask the above two questions, the answer is very simple, in the largest element of the time, we set up a small top heap of 10 elements, then the heap top element is certainly the smallest, and then take the remaining elements and heap top comparison, if larger than the heap, replace this element, and then adjust the heap, The top of the heap remains the smallest of the 10 elements after the adjustment, and the remaining elements are compared in turn.

Differences between heap sorting and direct insert sorting


In order to select the smallest record of a keyword from R[1..N], a n-1 comparison must be made, and then the smallest record of the keyword is selected in R[2..N], and n-2 is required. In fact, there are a number of comparisons that are likely to have been made in the previous n-1 comparison, but because the previous n-2 did not retain these comparisons, the comparison was performed repeatedly when the latter was sorted. Heap sorting saves some of the comparison results by using a tree structure, which reduces the number of comparisons.

"Go" heap sort

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.