This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.ts
More file actions
47 lines (44 loc) · 1.34 KB
/
select.ts
File metadata and controls
47 lines (44 loc) · 1.34 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
import { randInt } from "../../math/random";
function partition<T>(
arr: T[],
lo: number,
hi: number,
pivotIdx: number
): number {
[arr[hi - 1], arr[pivotIdx]] = [arr[pivotIdx], arr[hi - 1]];
let storeIdx = lo;
for (let i = lo; i < hi; ++i) {
if (arr[i] < arr[hi - 1]) {
[arr[i], arr[storeIdx]] = [arr[storeIdx], arr[i]];
++storeIdx;
}
}
[arr[hi - 1], arr[storeIdx]] = [arr[storeIdx], arr[hi - 1]];
return storeIdx;
}
/**
* Returns the element at index n (0-indexed) if the array were sorted.
* This also rearranges the elements in the specified array such that:
* - The element at index n is changed to the nth element if the array were sorted.
* - All elements before index n are less than or equal to the elements after index n.
* @param {T[]} arr The array that contains the element to be selected.
* @param {number} n The index of the element to be selected.
* @return {T} The nth element if the array were sorted.
*/
export function nthElement<T>(arr: T[], n: number): T {
if (n >= arr.length || n < 0) {
throw Error("Given index is out of bounds.");
}
let lo = 0;
let hi = arr.length;
while (lo < hi - 1) {
let pivotIdx = randInt(lo, hi);
pivotIdx = partition(arr, lo, hi, pivotIdx);
if (n < pivotIdx) {
hi = pivotIdx;
} else {
lo = pivotIdx;
}
}
return arr[lo];
}