-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradixSort.js
More file actions
148 lines (121 loc) · 3.4 KB
/
radixSort.js
File metadata and controls
148 lines (121 loc) · 3.4 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
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
///////////////////////////////////////////////////
//***************RADIX SORT HELPER 1 ************//
///////////////////////////////////////////////////
// getDigit function returns the digit in num at the given place value.
// getDigit(12345,0)//5
// //if the given place is 0, the number at the location 0 is 5.
//the place counts starts from the right to left. 1 -> 10 -> 100 -> 1000 -> 10000 ...
function getDigit(num, place){
return Math.floor(Math.abs(num) / Math.pow(10, place) % 10)
}
//getDigit(-7323,2)
//return Math.floor(Math.abs(num) / Math.pow(10, place) % 10)
//.abs return only absolute values
//7323 / Math.pow(10, 2) % 10
//7323 / 100 % 10
//73.23 % 10
//modulo is 3
console.log("getDigit 0", getDigit(7323, 0))//3
console.log("getDigit 1", getDigit(7323, 1))//2
console.log("getDigit 2", getDigit(7323, 2))//3
console.log("getDigit 3", getDigit(7323, 3))//7
// getDigit(12345,0)//5
// //if the given place is 0, the number at the location is 5
// getDigit(12345,1)//4
// getDigit(12345,2)//3
// getDigit(12345,3)//2
// getDigit(12345,4)//1
// getDigit(12345,5)//0
///////////////////////////////////////////////////
//***************RADIX SORT HELPER 2 ************/
///////////////////////////////////////////////////
//how many numbers of numbers are there in the longest number (lol)
function digitCount(num){
if(num === 0) return 1;
return Math.floor(Math.log10(Math.abs(num))) +1
}
console.log("digitCount", digitCount(7323))//4
///////////////////////////////////////////////////
//***************RADIX SORT HELPER 3 ************/
///////////////////////////////////////////////////
function mostDigits(nums){
let maxDigits = 0;
for(let i = 0; i < nums.length; i++ ){
maxDigits = Math.max(maxDigits, digitCount(nums[i]))
}
return maxDigits
}
console.log("mostDigit", mostDigits([23,334,21, 3421, 1453, 9898, 7323])) //4
///////////////////////////////////////////////////
//******************** RADIX SORT *****************/
///////////////////////////////////////////////////
function radixSort(nums){
let maxDigitCount = mostDigits(nums)
for(let k = 0; k < maxDigitCount; k++){
let digitBuckets = Array.from({length: 10}, () => [] )
console.log("digitBucket1", digitBuckets)
for(let i = 0; i < nums.length; i++){
let digit = getDigit(nums[i], k)
console.log("digit", digit)
digitBuckets[digit].push(nums[i])
}
nums = [].concat(...digitBuckets)
}
return nums
}
console.log(radixSort([23,334,21, 3421, 1453, 9898, 7323]))
// getDigit 0 3
// getDigit 1 2
// getDigit 2 3
// getDigit 3 7
// digitCount 4
// mostDigit 4
// digitBucket1 [
// [], [], [], [], [],
// [], [], [], [], []
// ]
// digit 3
// digit 4
// digit 1
// digit 1
// digit 3
// digit 8
// digit 3
// digitBucket1 [
// [], [], [], [], [],
// [], [], [], [], []
// ]
// digit 2
// digit 2
// digit 2
// digit 5
// digit 2
// digit 3
// digit 9
// digitBucket1 [
// [], [], [], [], [],
// [], [], [], [], []
// ]
// digit 0
// digit 4
// digit 0
// digit 3
// digit 3
// digit 4
// digit 8
// digitBucket1 [
// [], [], [], [], [],
// [], [], [], [], []
// ]
// digit 0
// digit 0
// digit 7
// digit 0
// digit 3
// digit 1
// digit 9
// [
// 21, 23, 334,
// 1453, 3421, 7323,
// 9898
// ]