-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquick-sort.js
51 lines (46 loc) · 1.06 KB
/
quick-sort.js
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
/*
* CODE: http://learnjswith.me/quick-sort-in-javascript/
*
* Quicksort is a divide and conquer algorithm. Quicksort first divides a
large array into two smaller sub-arrays: the low elements and the high
elements. Quicksort can then recursively sort the sub-arrays.
*
* MORE INFO: https://en.wikipedia.org/wiki/Quicksort
*/
function swap(arr, current, next) {
const temp = arr[current];
arr[current] = arr[next];
arr[next] = temp;
}
function partition(arr, left, right) {
const pivot = arr[Math.floor((right + left) / 2)];
let i = left;
let j = right;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr, j, i);
i++;
j--;
}
}
return i;
}
export function quickSort(arr, left = 0, right = arr.length - 1) {
let index;
if (arr.length > 1) {
index = partition(arr, left, right);
if (left < index - 1) {
quickSort(arr, left, index - 1);
}
if (index < right) {
quickSort(arr, index, right);
}
}
return arr;
}