Quick Sort Visualization
Speed:
Step: 0 / -1Algorithm Code
1function quickSort(arr, low = 0, high = arr.length - 1) {
2 if (low < high) {
3 // Partition the array and get the pivot index
4 const pivotIndex = partition(arr, low, high);
5
6 // Recursively sort the subarrays
7 quickSort(arr, low, pivotIndex - 1);
8 quickSort(arr, pivotIndex + 1, high);
9 }
10
11 return arr;
12 }
13
14 function partition(arr, low, high) {
15 // Choose the rightmost element as pivot
16 const pivot = arr[high];
17
18 // Index of smaller element
19 let i = low - 1;
20
21 for (let j = low; j < high; j++) {
22 // If current element is smaller than the pivot
23 if (arr[j] < pivot) {
24 // Increment index of smaller element
25 i++;
26 [arr[i], arr[j]] = [arr[j], arr[i]];
27 }
28 }
29
30 // Place pivot in its correct position
31 [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
32
33 // Return the pivot index
34 return i + 1;
35 }Visualization