-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradixSort.js
37 lines (31 loc) · 1.04 KB
/
radixSort.js
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
// Write a function that sorts an array using Radix Sort. Return a sorted array.
// This uses binary so it uses the size of the encoded digits.
let getDigit = (num, i) => {
return Math.floor(Math.abs(num) / Math.pow(10, i) % 10)
}
let digitCount = (num) => {
if (num === 0) return 1
return Math.floor(Math.log10(Math.abs(num))) + 1
}
let mostDigits = (nums) => {
let max = 0
for (let i = 0; i < nums.length; i++) {
max = Math.max(max, digitCount(nums[i]))
}
return max
}
let radixSort = (nums) => {
let maxDigitCount = mostDigits(nums)
for (let k = 0; k < maxDigitCount; k++) {
let digitBuckets = Array.from({length: 10}, () => [])
for (let i = 0; i < nums.length; i++) {
let digit = getDigit(nums[i], k)
digitBuckets[digit].push(nums[i])
}
nums = [].concat(...digitBuckets)
}
return nums
}
console.log(radixSort([23, 345, 6314, 734, 1, 4367, 0]))
// The amount of times this needs to happen depends on the largest number in the list.
// Time complexity is O(nk). Number of integers * length of those numbers.