java quicksort 2 pointer code example
Example: quicksort
function partition(list, start, end) {
const pivot = list[end];
let i = start;
for (let j = start; j < end; j += 1) {
if (list[j] <= pivot) {
[list[j], list[i]] = [list[i], list[j]];
i++;
}
}
[list[i], list[end]] = [list[end], list[i]];
return i;
}
function quicksort(list, start = 0, end = undefined) {
if (end === undefined) {
end = list.length - 1;
}
if (start < end) {
const p = partition(list, start, end);
quicksort(list, start, p - 1);
quicksort(list, p + 1, end);
}
return list;
}
quicksort([5, 4, 2, 6, 10, 8, 7, 1, 0]);