forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (46 loc) · 1.21 KB
/
Copy pathindex.js
File metadata and controls
50 lines (46 loc) · 1.21 KB
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
/**
* Sort array using quick sort
* @param {Array} arr - The array to partition.
* @param {Number} left - The left index of the array.
* @param {Number} right - The right index of the array.
* @return {Number} Sorted array.
**/
const quickSort = (arr, left = 0, right = arr.length - 1) => {
if (left < right) {
const pivot = partition(arr, left, right);
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
return arr;
};
/**
* Partition array using pivot
* @param {Array} arr - The array to partition.
* @param {Number} left - The left index of the array.
* @param {Number} right - The right index of the array.
* @return {Number} The index of the pivot.
*/
const partition = (arr, left, right) => {
const pivot = arr[right];
let i = left;
for (let j = left; j < right; j++) {
if (arr[j] <= pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, right);
return i;
};
/**
* Swap two elements in an array
* @param {Array} arr - The array to swap.
* @param {Number} i - The first index.
* @param {Number} j - The second index.
*/
const swap = (arr, i, j) => {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
};
module.exports = quickSort;