Sort an array in place with quicksort pivot partitioning
Quicksort is the classic divide-and-conquer sort: pick a pivot, move smaller elements to its left and larger ones to its right (partitioning), then sort each side the same way. Implement quickSort(array, compare) using quicksort.
quickSort(array, compare?) // => array (the same reference, now sorted)
array in place and return the same array. Do not build and return a new array.compare(a, b) works like the comparator of Array.prototype.sort: a negative result puts a before b, a positive result puts b before a, and 0 means they are equal.compare is omitted, sort numbers in ascending numeric order ((a, b) => a - b), not the string order that the built-in sort uses by default.Array.prototype.sort or Array.prototype.toSorted.const scores = [42, 7, 19, 7, 100, -3];
quickSort(scores); // => [-3, 7, 7, 19, 42, 100]
scores; // => [-3, 7, 7, 19, 42, 100] (sorted in place)
quickSort([10, 9, 1]); // => [1, 9, 10] (numeric, not '1', '10', '9')
const flights = [{ code: 'LH4', mins: 95 }, { code: 'BA2', mins: 40 }, { code: 'AF8', mins: 70 }];
quickSort(flights, (a, b) => b.mins - a.mins).map((f) => f.code);
// => ['LH4', 'AF8', 'BA2']
quickSort(['pear', 'Apple', 'fig'], (a, b) => a.localeCompare(b));
// => ['Apple', 'fig', 'pear']
quickSort([]); // => []