However, many books 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 grasp the quick sorting ideas and writing methods from this simple code.
Copy the code code as follows:
function quick_sort(list, start, end) {
if (start < end) {
var pivotpos = partition(list, start, end); //Find the base of quick sorting
quick_sort(list, start, pivotpos - 1); //Quick sort the left one
quick_sort(list, pivotpos + 1, end); //Quick sort the right one
}
}
//Adjust a sequence into two areas divided by the base number, one side is not less than the base number, and the other side is not greater than the base number
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 = list[start];
list[start] = list[pivotpos];
list[pivotpos] = tmp;
return pivotpos;
}
var list = [8,2,4,65,2,4,7,1,9,0,2,34,12];
quick_sort(list, 0, list.length);