-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathquick_sort.ts
44 lines (37 loc) · 1.07 KB
/
quick_sort.ts
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
function swap(arr: string[], i: number, j: number) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function partition(arr: string[], left: number, right: number): number {
// We are using the right most element as the pivot
const pivot = arr[right];
// This is the position where the pivot will eventually end up
let swapIndex = left - 1;
// Iterate over the arr partition and swap elements if required
for (let i = left; i < right; i++) {
if (arr[i] <= pivot) {
swapIndex++;
swap(arr, i, swapIndex);
}
}
// Finally place the pivot in the right place
swap(arr, swapIndex + 1, right);
return swapIndex + 1;
}
function quicksort(
arr: string[],
left: number = 0,
right: number = arr.length - 1
): string[] {
if (left >= right) {
return arr;
}
// Partition the array, this element is in the right place now
const pivot = partition(arr, left, right);
// Sort the left and right partitions and return the array
quicksort(arr, left, pivot - 1);
quicksort(arr, pivot + 1, right);
return arr;
}
export { quicksort };