The idea of a quick sort: (divided by rank)
(1) In the data set, select a number for comparison ("Datum" Pivot).
(2) Compare data collection traversal to datum data, less than to the left of datum data, and elements greater than datum to the right of datum data.
(3) for the two subsets to the left and to the right of the datum, recursively repeats the first and second steps until only one element is left in all the subsets.
JavaScript implementation
//1. Quick SortvarStartTime =NewDate (). GetTime ();//Start TimevarQuickSort =function(arr) {//Filter Conditions if(Arr.length <= 1) { returnarr; } //pivotindex Intermediate number, Pivot datum data (one data to compare) varPivotindex = Math.floor (arr.length/2), Pivot= Arr.splice (Pivotindex, 1) [0]; //define two empty arrays to store data after comparison with Datum data varLeftarr = [], Rightarr = []; //Cycle Comparison for(vari = 0; i < arr.length; i++) { if(Arr[i] <pivot) {Leftarr.push (arr[i])}Else{Rightarr.push (arr[i]); } } //recursively repeating this operation, Concat is to append the datum data with the larger data to the left array returnQuickSort (Leftarr). Concat ([pivot], rightarr);};//TestvarStartarr = [1, 22, 33, 44, 55, 66, 2];varEndarr =QuickSort (Startarr);//OutputvarEndTime =NewDate (). GetTime ();//Execution End TimeConsole.log (Endarr); Console.log (EndTime-StartTime + "MS");
Algorithm-Quick Sort (Quicksort)