Quick Sort Implementation

Source: Internet
Author: User

//This version is an optimized fast sorting algorithm for tuning. # include <iostream># include<vector>using namespacestd;intPartition (vector<int> &arr,intLowintHigh ) {    intPivot//location of the base record after partitioning    intPivotKey = Arr[low];//use the first value of the interval as a benchmark, or other means, such as three-digit     while(Low <High ) {         while(Low < High && Arr[high] >= pivotkey)//scan from right to left firsthigh--; if(Low < High)//every time there must be a judgment, because it is possible at this time to appear low>high situationarr[low++] =Arr[high];  while(Low < High && Arr[low] <= pivotkey)//and then scan from left to right .low++; if(Low <High ) Arr[high--] =Arr[low]; } Pivot= Low;//find the base positionArr[pivot] = PivotKey;//Base position Positioning    returnpivot;}//The thought of the typical divide-and-conquer lawvoidQuicksort (vector<int> &arr,intLowintHigh ) {    if(Low < High)//sort only if interval length is greater than 1 o'clock    {        intPivot = Partition (arr, low, high);//decomposition to find the benchmarkQuicksort (arr, Low, pivot-1);//recursive solution to left sub-rangeQuicksort (arr, pivot +1, high);//recursive solution to right sub-range    }}intmain () {vector<int>arr; inttemp;  while(Cin >>temp) {Arr.push_back (temp);//depositing numbers into an array} quicksort (arr,0, Arr.size ()-1);  for(auto I:arr) cout<< I <<Endl; return 0;}

//optimization of bubbling Algorithm implementation# include <iostream># include<vector>using namespacestd;voidBubblesort (vector<int> &arr) {    intI, J, Temp,len; Len=arr.size ();  for(i =0; I < Len-1; i++)//Outer loop controls the end position of the unordered area    {        BOOLFlag =true;  for(j = Len-1; J > i; j--)//the position of the inner layer to control each comparison        {            if(Arr[j] < arr[j-1]) {temp=Arr[j]; ARR[J]= Arr[j-1]; Arr[j-1] =temp; Flag=false; }        }        if(flag) Break; }}intmain () {vector<int>arr; inttemp;  while(Cin >>temp) {Arr.push_back (temp);//depositing numbers into an array} bubblesort (arr); cout<<"After sort:"<<Endl;  for(auto I:arr) cout<< I <<Endl; return 0;}

Quick Sort Implementation

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.