Three sorts: quick-row, merge, heap-row

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/LUO77/p/5798149.html

(a) Quick line

The quickest test is the most numerous. Before looking at the algorithm written by the Great God is very simple, thinking is also very good. He's been thinking about the fast-line. Pit-digging method.

Take the first element as a pacesetter element, i.e. dig a pit, then look for a smaller pits from behind, then form a pit, then look for a pits bigger than the pacesetter, and form a pit. ...... The last pit is filled with a pacesetter.

And then it's recursion. Again on the left of the pacesetter sort, right sort.

1 voidQSort (int* Num,intStartintend) {2     if(num = = null| | Start >=end)3         return;4     intTMP =Num[start];5     inti = start, j =end;6      while(i<j) {7          while(i<j&&num[j]>tmp) {8j--;9         }Ten         if(i<j) Onenum[i++] =Num[j]; A          while(i<j&&num[i]<tmp) { -i++; -         } the         if(i<j) -num[j--] =Num[i]; -     } -Num[i] =tmp; +QSort (num, start, I-1); -QSort (num, i +1, end); +}

Merge:

The idea of merging is to divide and conquer-the combination.

Divide first, then merge.

Divide thought is relatively simple, find middle, then divide a[start,,,,, Middle],a[middle+1...end]

For the left in recursive partitioning, divide until only one element is left, and then merge. The merge requires a temporary array. Merge is A[first...middle] and a[middle+1......end].

For the right in recursive partitioning, divide until only one element is left, and then merge.

The left and right are ordered, and then the two arrays are merged into an array. Finally, the entire array is in order. (process left, then handle right)

1 voidMergeintAintStartintMiddle,intLastint*tmp) {2 3     intI1 = start, J1 =Middle;4     intI2 = Middle+1, J2 =Last ;5     intindex =0;6      while(i1<=j1&&i2<=J2) {7         if(a[i1]<=A[i2])8tmp[index++] = a[i1++];9         Elsetmp[index++] = a[i2++];Ten     } One      while(I1 <=J1) { Atmp[index++] = a[i1++]; -     } -      while(I2 <=J2) { thetmp[index++] = a[i2++]; -     } -      for(inti =0; i<index; i++) { -A[start + i] =Tmp[i]; +     } -     return; + } A voidDivideintAintStartintEndint*tmp) { at     if(start<end) { -         intMiddle = (start + end)/2; - Divide (A, start, Middle, TMP); -Divide (A, middle+1, end, TMP); - Merge (A, start, middle, end, TMP); -     } in } - voidMergeSort (intAintsize) { to     if(A = = NULL | | size = =0|| Size = =1) +         return; -     int* TMP =New int[size]; theDivide (A,0, Size-1, TMP); *     Delete[] tmp; $     return;Panax Notoginseng}

Heap ordering (with maximum heap as an example):
1. First to build a maximum heap (starting from the size/2-1 location to maintain the heap, the leaf node by default is already a maximum heap, maintained to the root node, it has formed a maximum heap)
2. Swap the root node (at this time the root node is the maximum), and the last node, destroying the nature of the maximum heap, at this time continue to maintain the maximum heap (the process of maintaining the maximum heap is similar to the direct insertion sort, to find the appropriate location for maintenance point insertion, to ensure that the maximum heap nature is not destroyed)
3. Cycle through the last node and root node, each maintenance of the maximum heap size minus one (find the largest, find the second big, the next big ...) ), to the last to the root node, the sort is finished.

1 voidSwapint& A,int&b) {2A ^=b;3b ^=A;4A ^=b;5 }6 voidHeapadjust (intAintSizeintstart) {7     inti =start;8     intj =2* i +1;9     intTMP =A[i];Ten      while(J <=size) { One         if(j +1<= Size&&a[j +1]>A[j]) AJ + +; -         if(A[j] <=tmp) -              Break; theA[i] =A[j]; -i =J; -j =2* i +1; -     } +A[i] =tmp; -     return; + } A  at voidCreateheap (intAintsize) { -      for(inti = size/2-1; I >=0; i--) -Heapadjust (A, size-1, i); - } -  - voidHeapsort (intAintsize) { in     if(A = = NULL | | size = =0|| Size = =1) -         return; to createheap (A, size); +      for(inti = size-1; I >=1;) { -Swap (a[0], a[i--]); theHeapadjust (A, I,0); *     } $}

Three sorts: quick-row, merge, heap-row

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.