forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radixSort.go
76 lines (56 loc) · 1.37 KB
/
radixSort.go
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
package main
import (
"fmt"
"strconv"
)
// Finds the largest number in an array
func findLargestNum(array []int) int {
largestNum := 0
for i := 0; i < len(array); i++ {
if array[i] > largestNum {
largestNum = array[i]
}
}
return largestNum
}
func radixSort(array []int) []int {
largestNum := findLargestNum(array)
size := len(array)
significantDigit := 1
unSorted := make([]int, size, size)
// Loop until the largest significant digit is reached
for largestNum/significantDigit > 0 {
strconv.Itoa(significantDigit)
bucket := [10]int{0}
for i := 0; i < size; i++ {
bucket[(array[i]/significantDigit)%10]++
}
for i := 1; i < 10; i++ {
bucket[i] += bucket[i-1]
}
for i := size - 1; i >= 0; i-- {
bucket[(array[i]/significantDigit)%10]--
unSorted[bucket[(array[i]/significantDigit)%10]] = array[i]
}
for i := 0; i < size; i++ {
array[i] = unSorted[i]
}
significantDigit *= 10
}
return array
}
func main() {
unsortedList := []int{14, 10, 9, 18, 20, 5, 2, 7}
sortedList := radixSort(unsortedList)
fmt.Println("\nSorted List:", sortedList, "\n")
}
/*
Output -
Sorted List: [2 5 7 9 10 14 18 20]
Time Complexity:
1. O(n*k) in best case
2. O(n*k) in average case
3. O(n*k) in worst case
where n is the number of elements and k is the number of bits required to represent largest element in the array
Space Complexity: O(n+k)
*/