Basic implementation of fast sorting
Fast sorting algorithm is a kind of efficient sorting algorithm based on exchange, it adopts the idea of divide and conquer law :
1. Remove a number from the sequence as the base number (pivot, pivot).
2. Divide the array (partition), move the elements that are larger than the base number to the right of the pivot, and move the elements less than or equal to the base number to the left of the pivot.
3. Repeat the second division of the left and right sub-intervals until there is only one element for each sub-interval.
The most important step in the fast line is to divide it. The process of division is "digging pits" and "pits" in popular language.
Quick Sort Implementation one:
1 intPartitionintArr[],intLeftintRight//Find Base Number Division2 {3 inti = left +1 ;4 intj =Right ;5 inttemp =Arr[left];6 7 while(I <=j)8 {9 while(Arr[i] <temp)Ten { Onei++; A } - while(Arr[j] >temp) - { thej--; - } - if(I <j) -Swap (arr[i++], arr[j--]); + Elsei++; - } + swap (Arr[j], arr[left]); A returnJ; at - } - - voidQuick_sort (intArr[],intLeftintRight ) - { - if(Left >Right ) in return; - intj =partition (arr, left, right); toQuick_sort (arr, left, J-1); +Quick_sort (arr, j +1, right); -}
Quick Sort Implementation Method two:
1 voidQuickSort (intArray[],intStartintLast )2 {3 inti =start;4 intj =Last ;5 inttemp =Array[i];6 if(I <j)7 {8 while(I <j)9 {Ten // One while(I < J && Array[j]>=temp) Aj--; - if(I <j) - { theArray[i] =Array[j]; -i++; - } - + while(I < J && Temp >Array[i]) -i++; + if(I <j) A { atARRAY[J] =Array[i]; -j--; - } - - } - //Put the base number in the I position inArray[i] =temp; - //Recursive method toQuickSort (array, start, I-1); +QuickSort (array, i +1, last); - } the}
Fast sorting with C + + function template implementation
1Template<typename t>2 voidQuicksort (T data[],intFirstintLast )3 {4 intLower = first +1;5 intUpper =Last ;6Swap (Data[first], data[(first + last)/2]);7T bound =Data[first];8 while(Lower <=Upper)9 {Ten while(Data[lower] <bound) Onelower++; A while(Data[upper] >bound) -upper--; - if(Lower <Upper) theSwap (data[lower++], data[upper--]); - Elselower++; - } - swap (Data[upper], Data[first]); + if(First < Upper-1) -Quicksort (data, first, upper-1); + if(Upper +1<Last ) AQuicksort (data, Upper +1, last); at } - -template<classT> - voidQuicksort (T data[],intN) - { - intI, Max; in if(N <2) - return; to for(i =1, max =0; I < n; i++) + if(Data[max] <Data[i]) -Max =i; theSwap (Data[n-1], Data[max]); *Quicksort (data,0N2);// $}
Quick Sort main function test code
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<algorithm>#include<time.h>using namespacestd;voidPrintArray (intArray[],intLen) { for(inti =0; i < Len; i++) {cout<< Array[i] <<" "; } cout<<Endl;}intMainvoid){ Const intNUM =Ten; intArray[num] = {0 }; Srand ((unsignedint) Time (nullptr)); for(inti =0; i < NUM; i++) {Array[i]= rand ()% -+1; } cout<<"before sorting:"<<Endl; PrintArray (array, NUM); cout<<"after sorting:"<<Endl; Quicksort (Array,0, NUM-1); PrintArray (array, NUM); return 0;}
Quick Sort (c + + implementation)