Skip to main content

Quick Sort

Quick Sort is an efficient, divide-and-conquer sorting algorithm that picks a pivot element and partitions the array around it.

Time Complexity

  • Best: O(n log n)
  • Average: O(n log n)
  • Worst: O(n²)

Example

function quickSort(arr) {
if (arr.length <= 1) return arr;

const pivot = arr[arr.length - 1];
const left = arr.filter((el, idx) => el <= pivot && idx < arr.length - 1);
const right = arr.filter(el => el > pivot);

return [...quickSort(left), pivot, ...quickSort(right)];
}