//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