-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.js
More file actions
112 lines (83 loc) · 2.85 KB
/
QuickSort.js
File metadata and controls
112 lines (83 loc) · 2.85 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Array: Quick Sort
// Create a function that uses yesterday’s partition to sort an array in-place.
// With yesterday’s code plus a very few new lines, you will implement QuickSort!
// Time Complexity
// - Best: O(n log(n))
// - Average: O(n log(n))
// - Worst: O(n^2)
// Steps:
// - recursively partition the array
// - start by partitioning the full array (use the previously built partition algo)
// - then partition the left side of the array (left of new pivot idx)
// and the right side (right of new pivot idx), recursively
function quickSort(arr, left, right){
if(left === undefined){
left = 0;
}
if(right === undefined){
right = arr.length - 1;
}
//
if (left >= right) {
return;
}
const pivotIdx = partitionLomuto(arr, left, right);
quickSort(arr, left, pivotIdx - 1); // left of pivot
quickSort(arr, pivotIdx, right); // right of pivot
return arr;
}
const b = [1, 17, 12, 3, 9, 13, 21, 4, 27];
console.log(b.join(", "));
console.log(quickSort(b).join(", "));
// Array: Partition
// partition(arr, left, right){}
// partition(arr, left, right){}
// [99, 77, 66, 33, 22, 11 ...... 222, 111]
// https://opendsa-server.cs.vt.edu/embed/quicksortAV
// https://www.youtube.com/watch?v=ZZuD6iUe3Pc
// https://prod.liveshare.vsengsaas.visualstudio.com/join?E83A919698AC5B81F92DA897F8CCF644B91F
// Steps:
// 1. Pick an item out of the arr to be your pivot value
// - usually the middle item or the last item
// 2. Partition the array IN PLACE such that all values less than the pivot are to the left of it
// and all values greater than the pivot are to the right (not perfectly sorted)
// 3. return: new idx of the pivot value
// Params: arr, left, right
// - for now, left will be 0, and right will be the last idx
// - later these params will be used to specify a sub section of the array to partition
// stand-alone swap function
function swap(arr, i, j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
// Sir Charles Antony Richard Hoare partitioning scheme
function partitionHoare(arr, left, right) {
const pivot = arr[Math.floor((left + right) / 2)];
while (left <= right) {
while (arr[left] < pivot && left <= right) {
left++;
}
while (arr[right] > pivot) {
right--;
}
if (left <= right) {
[arr[left], arr[right]] = [arr[right], arr[left]];
left++;
right--;
}
}
// swap(arr, left, Math.floor((left + right)) / 2);
return left;
}
// Lomuto partitioning scheme, less efficient, easier to read
function partitionLomuto(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;
}