-
Notifications
You must be signed in to change notification settings - Fork 13
/
radix_sort.ts
68 lines (59 loc) · 1.6 KB
/
radix_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* This function returns the UTF16 char code of the character (if present).
* Otherwise returns 0.
*
* @param {string} str
* @param {number} pos
* @returns {number}
*/
function getCharCode(str: string, pos: number): number {
if (pos < str.length) {
return str.charCodeAt(pos);
}
return 0;
}
/**
* Returns the length of the longest string present in the array
*
* @param {string[]} arr
* @returns {number}
*/
function findMaxLength(arr: string[]): number {
let ans = -1;
for (let i = 0; i < arr.length; i++) {
ans = Math.max(ans, arr[i].length);
}
return ans;
}
function countingSort(arr: string[], pos: number): string[] {
const count: number[] = Array(65536).fill(0);
const sortedArr: string[] = Array(arr.length); // Sorted final array
// Store count of occurrences in count[]
for (let i = 0; i < arr.length; i++) {
const index = getCharCode(arr[i], pos);
count[index]++;
}
// Change count[i] so that it now contains
// the actual position of this character in sortedArr
for (let i = 1; i < count.length; i++) {
count[i] += count[i - 1];
}
// Build the sortedArr
for (let i = arr.length - 1; i >= 0; i--) {
const index = getCharCode(arr[i], pos);
sortedArr[count[index] - 1] = arr[i];
count[index]--;
}
// return the array
return sortedArr;
}
function radixSort(arr: string[]): string[] {
// Find the maximum length of string
const maxLength = findMaxLength(arr);
// Perform counting sort for every character.
for (let i = maxLength - 1; i >= 0; i--) {
arr = countingSort(arr, i);
}
return arr;
}
export { radixSort };