this article illustrates the way JavaScript implements fast sorting. Share to you for your reference, as follows:

: "thought through the divide and conquer idea, recursive method the data is divided into different sub sequences of

1. contain smaller elements and larger elements in the array to choose an element for the benchmark

2. to traverse the array, less than the benchmark the elements are moved to the left of the benchmark, is greater than the reference elements are moved to the right

3. benchmark on the basis of the left and right of the two sub sets, repeat the first two steps, until only a subset of all the elements have been" font-size: medium "

 function sqort Code: 

(ARR) if (arr.length===0) {{return} []; VAR left=[]; VAR right=[]; VAR pivot=arr[0]; / / (a reference to the first element) for(var i=1;i<arr.length;i++){ if(arr[i]<pivot){ left.push(arr[i]); }else{ right.push(arr[i]); } } return sqort(left).concat(pivot,qsort(right));//递归 } var a=[]; for (i=0;i<10;++i){ a[i]=Math.floor(Math.random()*100+1); } console.log(a); console.log(sqort(a)); //(基准以中间元素的情况) function sqort(arr){ if(arr.length<=1){ return arr; } var left=[]; var right=[]; var pivotIndex=Math.floor(arr.length/2); var pivot=arr.splice(pivotIndex,1)[0];//(基准以中间元素) for(var i=1;i<arr.length;i++){ if(arr[i]<pivot){ left.push(arr[i]); }else{ right.push(arr[i]); } } return sqort(left).concat(pivot,sqort(right));//递归 } var a=[12,34,23,78,34,26]; console.log(a); console.log(sqort(a));

for smaller and larger array array respectively (sqort) recursive call function, when the recursive end time, then an array with the reference small and large array connected together to form the final ordered array and returns.

online animation insert / choice / bubble / merge / Hill / quick sort algorithm process tools:
http://tools.jb51.net/aideddesign/paixu_ys

JavaScript more about related content interested readers can see the special station: "JavaScript ," math Usage Summary "JavaScript data structure and algorithm of , the" JavaScript ", an array of skills of "JavaScript sorting algorithm summary ", " JavaScript traversal algorithm Methods and skills summarize , JavaScript search algorithm summary, and JavaScript error and debugging skills summary, , ">JavaScript", hope that this article is helpful for everyone's program design.


This concludes the body part