forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radix_Sort.coffee
49 lines (33 loc) · 967 Bytes
/
Radix_Sort.coffee
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
Max_Element = (array) ->
max = array[0]
for i in [1...array.length]
if array[i] > max
max = array[i]
max
Counting_Sort = (array, exp) ->
size = array.length
output = Array(size).fill(0)
count = Array(10).fill(0)
for i in [0...size]
count[Math.floor(array[i] / exp) % 10] += 1
for i in [1...10]
count[i] += count[i - 1]
# Both a and b are inclusive when we use [a..b]
for i in [size - 1..0] by -1
output[count[Math.floor(array[i] / exp) % 10] - 1] = array[i]
count[Math.floor(array[i] / exp) % 10] -= 1
for i in [0...size]
array[i] = output[i]
Radix_Sort = (array) ->
maximum = Max_Element array
exp = 1
while Math.floor(maximum / exp) > 0
Counting_Sort array, exp
exp *= 10
0 # Return 0
array = [170, 45, 75, 90, 802, 24, 2, 66]
Radix_Sort array
console.log array
### Output
[ 2, 24, 45, 66, 75, 90, 170, 802 ]
###