1 functionquickSort (array, left, right) {2 //when the Left,right parameter is not given, the default value3 varleft = left | | 0,4right = right | | Array.length-1;5 if(Left >=Right ) {6 returnArray;7 }8 //index value, each time the first element of an array9 varindex =Array[left];Ten //Set Sentinel I and J One vari =Left , Aj =Right ; - //follow-up treatment when two sentinels do not meet - while(I! =j) { the //Let Sentinel J start running to the left until it finds a small number of peso values. - while(Array[j] >= index && i <j) { -j--; - } + //Let Sentry I run right until I find a large number of peso values - while(Array[i] <= index && i <j) { +i++; A } at //when Sentinel I is still on the left side of Sentinel J, it is exchanged - if(I <j) { - vart =Array[i]; -Array[i] =Array[j]; -ARRAY[J] =T; - } in } - //when Sentinel I and J meet, the index value is exchanged with the Sentinel toArray[left] =Array[i]; +Array[i] =index; - //recursive invocation, then processing the left and right parts of the index value the returnQuickSort (array, left, i-1); * returnQuickSort (Array, i + 1, right); $}
These days pondering the next algorithm, for a long time did not make this thing feel their programming ability is degraded, and took out practice, used to write with C, and now learn JS thinking to take this to achieve the next.
QuickSort by JavaScript