forked from jeantimex/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-sort-ii.js
80 lines (68 loc) · 1.94 KB
/
quick-sort-ii.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Quick Sort II
*
* Hoare's partition scheme
* Hoare's Partition Scheme works by initializing two indexes that start at two ends,
* the two indexes move toward each other until an inversion is (A smaller value on
* left side and greater value on right side) found. When an inversion is found, two
* values are swapped and process is repeated.
*
* Hoare's scheme is more efficient than Lomuto’s partition scheme because it does
* three times fewer swaps on average, and it creates efficient partitions even when
* all values are equal.
*/
const swap = (nums, i, j) => ([nums[i], nums[j]] = [nums[j], nums[i]]);
/**
* Hoare's partition scheme
* Hoare's Partition Scheme works by initializing two indexes that start at two ends,
* the two indexes move toward each other until an inversion is (A smaller value on
* left side and greater value on right side) found. When an inversion is found, two
* values are swapped and process is repeated.
*
* Hoare's scheme is more efficient than Lomuto’s partition scheme because it does
* three times fewer swaps on average, and it creates efficient partitions even when
* all values are equal.
*
* @param {number[]} nums
* @param {number} lo
* @param {number} hi
*/
const partition = (nums, lo, hi) => {
const pivot = lo + Math.floor((hi - lo) / 2);
let i = lo;
let j = hi;
while (i <= j) {
while (nums[i] < nums[pivot]) {
i++;
}
while (nums[j] > nums[pivot]) {
j--;
}
if (i <= j) {
swap(nums, i++, j--);
}
}
return i;
};
/**
* Quick sort helper - Returns sorted nums
*
* @param {number[]} nums
* @param {number} lo
* @param {number} hi
*/
const sort = (nums, lo, hi) => {
if (lo >= hi) {
return;
}
const pivot = partition(nums, lo, hi);
sort(nums, lo, pivot - 1);
sort(nums, pivot, hi);
};
/**
* Quick sort
*
* @param {number[]} nums
*/
const quickSort = nums => sort(nums, 0, nums.length - 1);
export default quickSort;