Sort (quick sort and heap sort) exercises

Source: Internet
Author: User

1#include <iostream>2 3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 //Karllen5 //46 7 voidQuickSort (int*a,intLowintHigh);//Quick Sort8 voidHeapsort (int*a,intn);//Heap Sort9 voidEleswap (int&a,int&AMP;B);//Exchanging DataTen intLeftindex (intindex);//returns the subscript of the left child node One intRightindex (intindex);//returns the subscript of the right child node A voidMakeheapmax (int*a,intlength);//Build a heap - voidMaxheapify (int*a,intindex);//down filter Maximum value - Static intHeapSize =0;//the scale of the heap the  - //Test - intMainintargcChar**argv) - { +     intA[] = {4,9,3,5,7,5,9,2,1,8,Ten}; -     intB[] = {4,9,3,5,7,5,9,2,1,8,Ten}; +     inti =0; AQuickSort (A,0,Ten); at      while(i< One) -     { -std::cout<<a[i]<<" "; -++i; -     } -     intj =0; inHeapsort (b, One); -      while(j< One) to     { +std::cout<<b[j]<<" "; -++J; the     } *     return 0; $ }Panax Notoginseng //Quick Sort - voidQuickSort (int*a,intLow,intHigh ) the { +     if(Low <High ) A{//Natural recursive return condition the         intPivot, I, J; +Pivot = A[low];//pivot point equals first element -i =Low ; $j =High ; $          while(i<j) -         { -              while(I<j && a[j]>=pivot) the             { ---J;Wuyi             } the             if(i<j) -a[i++] =A[j]; Wu              while(I<j && a[i]<=pivot) -             { About++i; $             } -             if(i<j) -a[j--] =A[i]; -         } AA[i] = pivot;//ordered Benchmark +QuickSort (a,low,i-1);//recursive sorting is less than the part of the pivot point theQuickSort (a,i+1, high);//recursive sorting is greater than the part of the pivot point -     } $ } the //Swap Functions the voidEleswap (int&a,int&b) the { the     inttemp =A; -A =b; inb =temp; the } the //Zoozi About intLeftindex (intindex) the { the     return(index<<1)+1; the } + //Right Sub-order - intRightindex (intindex) the {Bayi     return(index<<1)+2; the } the  - //priority large-heap filtering algorithm - voidMaxheapify (int*a,intindex) the { the     intLargestindex =0; the     intleft =Leftindex (index); the     intright =Rightindex (index); -     if(Left < heapsize && a[left]>A[index]) the     { theLargestindex =Left ; the     }94     Else the     { theLargestindex =index; the     }98     if(Right < HeapSize && a[right]>A[largestindex]) About     { -Largestindex =Right ;101     }102     if(Largestindex! =index)103     {104 Eleswap (A[index],a[largestindex]); the maxheapify (a,largestindex);106     }107 }108 109 //Build a heap the voidMakeheapmax (int*a,intlength)111 { theHeapSize =length;113      for(inti = ((length>>1)-1); i>=0;--i) the     { the maxheapify (a,i); the     }117 }118 119 //Heap Sort - voidHeapsort (int*a,intN)121 {122 Makeheapmax (a,n);123      for(inti = n-1; i>=1;--i)124     { theEleswap (a[0],a[i]);126--heapsize;127Maxheapify (A,0); -     }129}

Heap sorting is one embodiment of the priority queue, the implementation principle, the highest priority and the last element of the exchange, that is, the largest or smallest to the order part, and then adjust the unordered part of the heap to re-form, continue to perform the above repeated operations, until the unordered part of the element is 1, sorting can be completed.

The principle of fast sorting is to use the characteristics of the pivot point, the recursive sort is larger than the pivot point and smaller than the Axis point, only to the axis of the elements on both sides of the 1, when the natural order, recursive return, fast row can be achieved. The key part, the pivot point.

Sort (quick sort and heap sort) exercises

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.