Here is my ES6 implementation of the QuickSort Algorithm.

If you need more information on how Quicksort works, check out this HackerRank video.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const quicksort = (arr) => {
quicksortStep(arr, 0, arr.length - 1);
};
const quicksortStep = (arr, left, right) => {
if (left >= right) {
return;
}
const pivot = Math.floor((left + right) / 2);
const index = partition(arr, left, right, arr[pivot]);
quicksortStep(arr, left, index - 1);
quicksortStep(arr, index, right);
};
const partition = (arr, left, right, pivot) => {
while (left <= right) {
while (arr[left] < pivot) {
left = left + 1;
}
while (pivot < arr[right]) {
right = right - 1;
}
if (left <= right) {
swap(arr, left, right);
left = left + 1;
right = right - 1;
}
}
return left;
};
const swap = (arr, i, j) => {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
const arr1 = [3,4,5,7,3,1,3,4,6,8,3,1,1,4,7,9,3];
quicksort(arr1);
console.log(arr1);
const arr2 = [];
quicksort(arr2);
console.log(arr2);
const arr3 = [3];
quicksort(arr3);
console.log(arr3);
const arr4 = [4,3];
quicksort(arr4);
console.log(arr4);
const arr5 = [5,4,3];
quicksort(arr5);
console.log(arr5);