-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-select.mjs
253 lines (232 loc) · 4.98 KB
/
quick-select.mjs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { randomInt } from 'node:crypto'
import { bench, group, run } from 'tatami-ng'
/**
* @param numberOfWorkers
* @param maxNumberOfTasksPerWorker
* @returns
*/
function generateRandomTasksMap (
numberOfWorkers,
maxNumberOfTasksPerWorker = 10
) {
const tasksArray = []
for (let i = 0; i < numberOfWorkers; i++) {
const task = [i, randomInt(maxNumberOfTasksPerWorker)]
tasksArray.push(task)
}
return new Map(tasksArray)
}
const tasksMap = generateRandomTasksMap(60, 20)
/**
* @param tasksMap
* @returns
*/
function arraySortSelect (tasksMap) {
const tasksArray = Array.from(tasksMap)
return tasksArray.sort((a, b) => {
if (a[1] < b[1]) {
return -1
} else if (a[1] > b[1]) {
return 1
}
return 0
})[0]
}
/**
* @param tasksMap
* @returns
*/
function loopSelect (tasksMap) {
let minKey
let minValue = Number.POSITIVE_INFINITY
for (const [key, value] of tasksMap) {
if (value === 0) {
return key
} else if (value < minValue) {
minKey = key
minValue = value
}
}
return [minKey, minValue]
}
const defaultComparator = (a, b) => {
return a < b
}
const defaultPivotIndexSelect = (leftIndex, rightIndex) => {
return leftIndex + Math.floor((rightIndex - leftIndex) / 2)
}
const randomPivotIndexSelect = (leftIndex, rightIndex) => {
return randomInt(leftIndex, rightIndex)
}
/**
* @param array
* @param leftIndex
* @param rightIndex
* @param pivotIndex
* @param compare
* @returns
*/
function partition (
array,
leftIndex,
rightIndex,
pivotIndex,
compare = defaultComparator
) {
const pivotValue = array[pivotIndex]
swap(array, pivotIndex, rightIndex)
let storeIndex = leftIndex
for (let i = leftIndex; i < rightIndex; i++) {
if (compare(array[i], pivotValue)) {
swap(array, storeIndex, i)
storeIndex++
}
}
swap(array, rightIndex, storeIndex)
return storeIndex
}
/**
* @param tasksMap
* @returns
*/
function quickSelectLoop (tasksMap) {
const tasksArray = Array.from(tasksMap)
return selectLoop(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => {
return a[1] < b[1]
})
}
/**
* @param tasksMap
* @returns
*/
function quickSelectLoopRandomPivot (tasksMap) {
const tasksArray = Array.from(tasksMap)
return selectLoop(
tasksArray,
0,
0,
tasksArray.length - 1,
(a, b) => {
return a[1] < b[1]
},
randomPivotIndexSelect
)
}
/**
* @param tasksMap
* @returns
*/
function quickSelectRecursion (tasksMap) {
const tasksArray = Array.from(tasksMap)
return selectRecursion(tasksArray, 0, 0, tasksArray.length - 1, (a, b) => {
return a[1] < b[1]
})
}
/**
* @param tasksMap
* @returns
*/
function quickSelectRecursionRandomPivot (tasksMap) {
const tasksArray = Array.from(tasksMap)
return selectRecursion(
tasksArray,
0,
0,
tasksArray.length - 1,
(a, b) => {
return a[1] < b[1]
},
randomPivotIndexSelect
)
}
/**
* @param array
* @param k
* @param leftIndex
* @param rightIndex
* @param compare
* @param pivotIndexSelect
* @returns
*/
function selectLoop (
array,
k,
leftIndex,
rightIndex,
compare = defaultComparator,
pivotIndexSelect = defaultPivotIndexSelect
) {
while (true) {
if (leftIndex === rightIndex) return array[leftIndex]
let pivotIndex = pivotIndexSelect(leftIndex, rightIndex)
pivotIndex = partition(array, leftIndex, rightIndex, pivotIndex, compare)
if (k === pivotIndex) {
return array[k]
} else if (k < pivotIndex) {
rightIndex = pivotIndex - 1
} else {
leftIndex = pivotIndex + 1
}
}
}
/**
* @param array
* @param k
* @param leftIndex
* @param rightIndex
* @param compare
* @param pivotIndexSelect
* @returns
*/
function selectRecursion (
array,
k,
leftIndex,
rightIndex,
compare = defaultComparator,
pivotIndexSelect = defaultPivotIndexSelect
) {
if (leftIndex === rightIndex) return array[leftIndex]
let pivotIndex = pivotIndexSelect(leftIndex, rightIndex)
pivotIndex = partition(array, leftIndex, rightIndex, pivotIndex, compare)
if (k === pivotIndex) {
return array[k]
} else if (k < pivotIndex) {
return selectRecursion(array, k, leftIndex, pivotIndex - 1, compare)
} else {
return selectRecursion(array, k, pivotIndex + 1, rightIndex, k, compare)
}
}
/**
* @param array
* @param index1
* @param index2
*/
function swap (array, index1, index2) {
const tmp = array[index1]
array[index1] = array[index2]
array[index2] = tmp
}
group('Quick select', () => {
bench('Loop select', () => {
loopSelect(tasksMap)
})
bench('Array sort select', () => {
arraySortSelect(tasksMap)
})
bench('Quick select loop', () => {
quickSelectLoop(tasksMap)
})
bench('Quick select loop with random pivot', () => {
quickSelectLoopRandomPivot(tasksMap)
})
bench('Quick select recursion', () => {
quickSelectRecursion(tasksMap)
})
bench('Quick select recursion with random pivot', () => {
quickSelectRecursionRandomPivot(tasksMap)
})
})
await run({
units: true,
})