-
Notifications
You must be signed in to change notification settings - Fork 13
/
random_sort.ts
114 lines (99 loc) · 2.44 KB
/
random_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
interface IStringHash {
str: string;
count: number;
hash: number;
incrementCount(): number;
decrementCount(): number;
}
class StringHash implements IStringHash {
str: string;
count: number;
hash: number;
constructor(str: string, count: number, hash: number) {
this.str = str;
this.count = count;
this.hash = hash;
}
incrementCount(): number {
this.count++;
return this.count;
}
decrementCount(): number {
this.count--;
return this.count;
}
}
/**
* Computes the Hash value of a given string using
* Polynomial Rolling Hash Function.
*
* @param {string} str
* @returns {number}
*/
function computeHash(str: string): number {
const prime = 31;
const mod = 10e9 + 9;
let hashValue = 0;
let power = 1;
for (let i = 0; i < str.length; i++) {
const code = str.codePointAt(i);
if (code !== undefined) {
hashValue = (hashValue + code * power) % mod;
}
power = (power * prime) % mod;
}
return hashValue;
}
/**
* Given an array of number, this function randomly shuffles it's content and
* returns the new shuffled array.
*
* @param {number[]} arr
* @returns {number[]}
*/
function shuffleArray(arr: number[]): number[] {
for (let i = 0; i < arr.length; i++) {
// Generate random index to swap from
const idx = Math.floor(Math.random() * (i + 1));
const temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
return arr;
}
function randomSort(arr: string[]): string[] {
// Storing hash value and corresponding information
const map = new Map<number, IStringHash>();
for (let i = 0; i < arr.length; i++) {
const hash = computeHash(arr[i]);
// If hash already exists
if (!map.has(hash)) {
map.set(hash, new StringHash(arr[i], 1, hash));
}
// Otherwise increment the count
else {
map.get(hash)!.incrementCount();
}
}
const hashArr = shuffleArray([...map.keys()]); // Shuffle the hash values
const newArray: string[] = [];
for (let i = 0; i < hashArr.length; i++) {
const hash = hashArr[i];
if (map.has(hash)) {
// Get the element and push the string `count` times
const elem = map.get(hash)!;
let count = elem.count;
const str = elem.str;
while (count > 0) {
newArray.push(str);
count--;
}
map.delete(hash);
} else {
// Should never happen
throw new Error('Some error occurred');
}
}
return newArray;
}
export { randomSort };