Quicksort Algorithm

A divide-and-conquer sorting algorithm with O(n log n) average time complexity

45
32
8
27
15
41
19
35
4
23
UnsortedPivotComparingSwappingSorted
1function quicksort(arr, low, high) {
2  if (low < high) {
3    const pivotIndex = partition(arr, low, high);
4    quicksort(arr, low, pivotIndex - 1);
5    quicksort(arr, pivotIndex + 1, high);
6  }
7}
8
9function partition(arr, low, high) {
10  const pivot = arr[low];
11  let i = low + 1;
12  for (let j = low + 1; j <= high; j++) {
13    if (arr[j] < pivot) {
14      [arr[i], arr[j]] = [arr[j], arr[i]];
15      i++;
16    }
17  }
18  [arr[low], arr[i - 1]] = [arr[i - 1], arr[low]];
19  return i - 1;
20}