Home  >  Article  >  Web Front-end  >  js quick sort implementation code_javascript skills

js quick sort implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:161036browse

However, there are many books that do not explain it very clearly, and different teaching materials have different implementation methods.
I will write down the simplest quick sorting idea here for your reference.

I hope that no matter what language you use, you can easily master the quick sorting ideas and writing methods from this simple code

Copy the code The code is as follows:

function quick_sort(list, start, end) {
if (start < end) {
var pivotpos = partition(list, start, end) ; //Find the base of quick sort
quick_sort(list, start, pivotpos - 1); //Quick sort the left one once
quick_sort(list, pivotpos 1, end); //Quick sort the right one Arrange once
}
}


//Adjust a sequence into two areas divided by the base, with one side not less than the base and the other not greater than the base
function partition(list, start, end) {
var pivotpos = start;
var pivot = list[start];
var tmp;
for(var i = start 1; i <= end ; i ) {
if (list[i] < pivot) {
tmp = list[i];
pivotpos = 1;
list[i] = list[pivotpos];
           list[pivotpos] = tmp;                                              = tmp;
return pivotpos;
}

var list = [8,2,4,65,2,4,7,1,9,0,2,34,12 ];

quick_sort(list, 0, list.length);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn