forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRadix_Sort.rb
50 lines (38 loc) · 787 Bytes
/
Radix_Sort.rb
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
def counting_sort(input_arr, exp)
count_arr = Array.new(10, 0)
result = Array.new(input_arr.size)
m = 10 ** (exp+1)
n = 10 ** exp
input_arr.each do |item|
count_arr[(item % m) / n] += 1
end
for i in 1...10
count_arr[i] = count_arr[i-1] + count_arr[i]
end
i = input_arr.size - 1
until i < 0 do
item = input_arr[i]
count_arr[(item % m) / n] -= 1
result[count_arr[(item % m) / n]] = item
i -= 1
end
result
end
def radix_sort(arr)
maximum = arr.max
exp = 0
until maximum == 0 do
exp += 1
maximum = maximum / 10
end
for i in 0...exp do
arr = counting_sort(arr, i)
end
arr
end
array = [170, 45, 75, 90, 802, 24, 2, 66]
array = radix_sort(array)
print array
puts
# Output
# [2, 24, 45, 66, 75, 90, 170, 802]