Select Sort Heap Sort

Source: Internet
Author: User

/*
* Select sort of heap sort
* Set up a fully binary tree according to the sequential storage of the complete binary tree
* If Dagen: L (i) >=l (2*i), L (i) >=l (2*i+1)
* If small Gan: L (i) <=l (2*i), L (i) <= (2*i+1)
* At this point, the initial heap is built and the next job is to make adjustments
* adjusted is from [n/2]~1.
* For Dagen: For the [N/2] element, if it is smaller than the larger of the left and right children, exchange
* Similarly to other
* void Buildheap (ET a[],int len) {
* for (int i=n/2;i>0;i--) {
* Adjustdown (A[],i,len);
* }
* void Adjustdown (ET a[],int k,int len) {
* A[0]=a[k];
* for (I=2*K;I<=LEN;I=I*2) {
* if (i<len&&a[i]<a[i+1])
* i++;
* if (A[0]>a[i])
* break;//sorting success, jumping out of the loop
* else{
* A[k]=a[i];//a[i] upside down, anyway a[k] value has been assigned, with the
* K=I; At the same time the new K to be sorted is I, to continue downward adjustment
* }
* }
* a[k]=a[0];//Final position
* }

* The following is the complete heap sequencing process
* After we have built a true sense of the right, we also need to output it, so that we can visually see the results of the algorithm processing
* While the output of a heap sort, usually the top element of the output heap, usually feeds the bottom of the heap to the heap top, but at this point it must not satisfy the nature of the heap,
* So it is necessary to re-adjust the heap at this time, after the adjustment is finished, in the output, loop so
* void Heapsort (ET a[],int len) {
* BULIDHEAP (A,len);
* for (i=len;i>1;i--) {
* Print (a[1]);//output heap top element
* Swap (a[len],a[1]);//Change the last heap element to the top of the heap
* Adjustdown (a,1,len-1);//After an element is output, len-1 is left
*
* }
*
* }

The time complexity of heap sequencing is related to the resulting tree shape

The build time is O (n), after which the n-1 is adjusted (half when the heap is built, and half in the output). ), the time of each adjustment is related to the height of the tree, that is O (h), and because it is a complete binary tree, so the final time complexity is O (NLOGN)
*
* }
*/

Select Sort Heap Sort

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.