forked from basarat/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.ts
More file actions
45 lines (37 loc) 路 1.3 KB
/
Copy pathquickSort.ts
File metadata and controls
45 lines (37 loc) 路 1.3 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
/**
* Sorts an array using quick sort
*/
export function quickSort(array: number[]): number[] {
array = array.slice();
partition(array, 0, array.length);
return array;
}
/**
* Partitions the [start, before) index of the array
*/
function partition(array: number[], start: number, before: number): void {
const length = before - start;
/** Terminate the recursion */
if (length <= 1) return;
/** Randomly select a pivot and move it to the head of the array */
const pivotIndex = start + Math.floor(Math.random() * length);
[array[start], array[pivotIndex]] = [array[pivotIndex], array[start]];
/** The first element is our pivot */
const pivot = array[start];
let pivotRank = start;
/** Loop through all the elements, partitioning around the pivot */
for (let index = start + 1; index < before; index++) {
if (array[index] < pivot) {
pivotRank++;
[array[index], array[pivotRank]] = [array[pivotRank], array[index]];
}
}
/** Finally put the pivot at its rightfull place */
if (pivotRank !== start) {
[array[pivotRank], array[start]] = [array[start], array[pivotRank]];
}
/** Partition all the elements less than the pivot */
partition(array, start, pivotRank);
/** Partition all the elements more than the pivot */
partition(array, pivotRank + 1, before);
}