-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathquicksort.js
48 lines (40 loc) · 1.09 KB
/
quicksort.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
/*
Quick sort
PIVOT!
- the runtime of quick sort depends on pivot sort
- quicksort is in place
- at the end function returns the index of pivot
Quicksort -
- call the pivot helper on the array
- when the helper returns the updated pivot index, recursively call the pivot helper on the subarray to the left of the
index and then to the right of the index.
- base case occurs when subarray has less than 2 elements
*/
function swap(array, i, j) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
function pivot(arr, start = 0, end = arr.length -1) {
let pivot = arr[start];
let swapIndex = start;
for(let i= start+1; i<arr.length; i++) {
if(pivot > arr[i]) {
swapIndex++;
swap(arr, swapIndex, i);
}
}
swap(arr, start, swapIndex);
return swapIndex;
}
function quickSort(arr, left =0, right = arr.length-1) {
if(left < right) {
let pivotIndex = pivot(arr, left, right);
//left
quickSort(arr, left, pivotIndex - 1);
//right
quickSort(arr, pivotIndex + 1, right);
}
return arr;
}
console.log(quickSort([1009, 8, -2, 1, 5, 7, 6, 3]));