Sort by, sort by selection, sorting by heap

Source: Internet
Author: User

Text description

In the heap sort, the data to be sorted can also be represented by a complete binary tree, and the values of all non-terminal nodes of the complete binary tree are not greater than (or less than) the values of their left and right child nodes. Thus, if the sequence {k1, K2, ..., kn} is a heap, then the top element of the heap (or the root of a complete binary tree) must be the minimum (or maximum) of n elements in the sequence.

If, after the minimum value of the top of the output heap, the sequence of the remaining n-1 elements is re-built into a heap, the secondary decimal value of n elements is obtained. With this repeated execution, an ordered sequence can be obtained, a process called heap ordering.

Thus, the implementation of heap sequencing requires solving two problems: (1) How to build a heap from an unordered sequence? (2) How to adjust the remaining elements to become a new heap after the top element of the output heap?

First of all, consider the question of (2), assuming that there is a maximum heap (the heap top element is the maximum value of the heap) after the output of the top element of the heap, the last element in the heap instead of the left and right subtree of the root node is a heap, you only need to adjust from top to bottom. The adjustment process called the top-to-leaf of the heap is "screened".

Again, in question (1), the process of building a heap of unordered sequences is a process of "sifting" over and over again. If this sequence is considered to be a complete binary tree, then the last non-terminal node is the [N/2] element, so that the "filter" simply starts with the first [N/2] element.

Algorithm analysis

  Heap sorting has been improved in tree-selection sorting, and the sorting ratio of tree-shape selection, heap sorting can reduce the auxiliary space, avoid and the "maximum" redundancy comparison and other advantages.

Heap sequencing in the worst case, the time complexity is also nlogn. This is the biggest advantage of heap sorting relative to fast sorting (fast sorting is the worst case time complexity is n*n, but the average performance is Nlogn).

Heap sorting requires only a record size of secondary space for exchange.

Heap ordering is an unstable sort method.

In addition, heap sorting methods are not advocated for files with fewer records, but are very effective for n large files because their run time is mainly spent on the repeated "filtering" when establishing the initial heap and adjusting the new heap.

Code implementation

1#include <stdio.h>2#include <stdlib.h>3#include <math.h>4 /*5 * Double log2 (double x); Logarithm with base 26 * Double ceil (double x); Take the whole7 * Double floor (double x); Remove the entire8 * Double fabs (double x); Take absolute value9  */Ten  One #defineDEBUG A  - #defineEQ (A, B) ((a) = = (b)) - #defineLT (A, B) ((a) < (b)) the #defineLQ (A, B) ((a) <= (b)) -  - #defineMAXSIZE 100 - #defineINF 1000000 +typedefintKeyType; -typedefCharInfoType; +typedefstruct{ A KeyType key; at InfoType Otherinfo; - }redtype; -  -typedefstruct{ -Redtype r[maxsize+1]; -     intlength; in }sqlist; -  to voidprintlist (sqlist L) { +     inti =0; -printf"Subscript Value:"); the      for(i=0; i<=l.length; i++){ *printf"[%d]", i); $     }Panax Notoginsengprintf"\ n Keyword:"); -      for(i=0; i<=l.length; i++){ the         if(EQ (L.r[i].key, INF)) { +printf"%-3c",'-'); A}Else{ theprintf"%-3d", L.r[i].key); +         } -     } $printf"\ n Other values:"); $      for(i=0; i<=l.length; i++){ -printf"%-3c", l.r[i].otherinfo); -     } theprintf"\ n"); -     return ;Wuyi } the  - //The heap uses sequential storage representations Wu typedef sqlist Heaptype; -  About /* $ * H->r[s,..., m] are known to meet definitions except H->r[s].key - * The Chinese book Adjustment h-r[s] keyword, so that h->r[s,..., m] to become a big top heap (on which - * Record Keywords) -  */ A voidHeapadjust (Heaptype *h,intSintm) + { theRedtype rc = h->R[s]; -     intj =0; $     //filter down the key for the larger child the      for(j=2*s; j<=m; j*=2){ the         //J is the subscript for the larger record of key the         if(J<m && lt (H->r[j].key, h->r[j+1].key)) thej+=1; -         //RC should be inserted on position s in         if(!lt (Rc.key, h->R[j].key)) the              Break; theH->r[s] = h->R[j]; Abouts =J; the     } the     //Insert theH->r[s] =RC; + } -  the /*Bayi * Heap Ordering of sequential table H the  */ the voidHeapsort (Heaptype *H) - { -     inti =0; the     //put h->r[1,..., H->length] into a big top pile the      for(i=h->length/2; i>=1; i--){ theHeapadjust (H, I, h->length); the     } - #ifdef DEBUG theprintf"an initial large top heap is built from an unordered sequence: \ n"); thePrintlist (*H); the #endif94 redtype tmp; the      for(i=h->length; i>1; i--){ the         //swaps the last record in the heap top record and the current unsorted subsequence h->r[1,..., i] theTMP = h->r[1];98h->r[1] = h->R[i]; AboutH->r[i] =tmp; -         //resize h->r[1,..., i-1] to a large top heap101Heapadjust (H,1, I-1);102 #ifdef DEBUG103printf"adjust the elements of 1 to%d to make them a big top heap: \ n", I-1);104Printlist (*H); the #endif106     }107 }108 109 intMainintargcChar*argv[]) the {111     if(ARGC <2){ the         return-1;113     } the Heaptype H; the     inti =0; the      for(i=1; i<argc; i++){117         if(i>MAXSIZE)118              Break;119H.r[i].key =atoi (Argv[i]); -H.r[i].otherinfo ='a'+i-1;121     }122H.length = (I-1);123h.r[0].key =0;124h.r[0].otherinfo ='0'; theprintf"input data: \ n");126 printlist (H);127     //sequencing the sequential table H as a heap -Heapsort (&H);129     return 0; the}
Heap Sort

Run

Sort by, sort by selection, sorting by heap

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.