-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
38 lines (32 loc) · 1.13 KB
/
index.ts
File metadata and controls
38 lines (32 loc) · 1.13 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
// Counting Sort
// Time: O(n + k) where k is the range of the data
// Space: O(k)
// NOTE: Only supports positive numbers
export function countingSort(arrOfValues: number[]): number[] {
// find the max value in the array
const max = Math.max(...arrOfValues);
// max a counts array with the max number + 1 and fill with zeroes
let countsArr: Array<number> = Array(max + 1).fill(0);
// loop through array of values and increment
for (let i = 0; i < arrOfValues.length; i += 1) {
// get the current value in the array of values
const currentValue = arrOfValues[ i ];
// find the index of the current value
// for instance if the current value is 5 you
// want to increase the value at the index of 5
countsArr[ currentValue ] += 1;
}
let k = 0;
// loop until the max value we found
for (let j = 0; j <= max; j += 1) {
// if the value at the j index is great than zero
while (countsArr[ j ] > 0) {
// modify the original array at the k position with j
arrOfValues[ k ] = j;
k += 1;
// decrement the counts array at j
countsArr[ j ] -= 1;
}
}
return arrOfValues;
}