JavaScript performs quick sorting and javascript performs sorting.
This example describes how to implement quick sorting in JavaScript. We will share this with you for your reference. The details are as follows:
Thoughts:
Data is divided into different subsequences that contain smaller elements and larger elements by means of grouping and recursion.
1. Select an element as the benchmark in the array.
2. traverse the array. All elements smaller than the benchmark are moved to the left of the benchmark, and all elements larger than the benchmark are moved to the right of the benchmark.
3. Repeat the first two steps for the left and right subsets of the benchmark until only one element is left for all subsets.
Implementation Code:
Function sqort (arr) {if (arr. length = 0) {return [];} var left = []; var right = []; var comment = arr [0]; // (reference with the first element) for (var I = 1; I <arr. length; I ++) {if (arr [I] <distinct) {left. push (arr [I]);} else {right. push (arr [I]);} return sqort (left ). concat (histogram, qsort (right); // recursion} var a = []; for (I = 0; I <10; ++ I) {a [I] = Math. floor (Math. random () * 100 + 1);} console. log (a); console. log (sqort (a); // (when the base is an intermediate element) function sqort (arr) {if (arr. length <= 1) {return arr;} var left = []; var right = []; var comment tindex = Math. floor (arr. length/2); var partition = arr. splice (tindex, 1) [0]; // (base with intermediate element) for (var I = 1; I <arr. length; I ++) {if (arr [I] <distinct) {left. push (arr [I]);} else {right. push (arr [I]);} return sqort (left ). concat (iterator, sqort (right); // recursion} var a = [,]; console. log (a); console. log (sqort ());
Note:Recursively call small and large arrayssqort()Function. When the recursion ends, a small array is connected with the benchmark and a large array to form the final ordered array and return it.
PS: here we recommend a demo tool for sorting for your reference:
Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys