Skip to content

Commit 6ab41aa

Browse files
author
Thukor
committed
Finished Radix Sort Implementation
1 parent df9e4f6 commit 6ab41aa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Radix-Sort/radixSort

19.8 KB
Binary file not shown.

Radix-Sort/radixSort.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func radixSort(inout arr: [Int] ) {
2+
3+
let radix = 10
4+
var done = false
5+
var index: Int
6+
var digit = 1
7+
8+
while !done {
9+
done = true
10+
11+
var buckets: [[Int]] = []
12+
13+
for _ in 1...radix {
14+
buckets.append([])
15+
}
16+
17+
18+
for number in arr {
19+
index = number / digit
20+
buckets[index % radix].append(number)
21+
if done && index > 0 {
22+
done = false
23+
}
24+
}
25+
26+
var i = 0
27+
28+
for j in 0..<radix {
29+
let bucket = buckets[j]
30+
for number in bucket {
31+
arr[i] = number
32+
i += 1
33+
}
34+
}
35+
36+
digit *= radix
37+
}
38+
}
39+
40+
var a: [Int] = [0, 69, 28, 14, 32, 1, 1, 1111, 1111111, 55, 123, 236626456256, 9393, 23, 66]
41+
radixSort(&a)
42+
print(a)

0 commit comments

Comments
 (0)