Skip to content

Commit 0837113

Browse files
authored
Add files via upload
upload a radix sort implement,faster than original code
1 parent 69cb42e commit 0837113

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Radix Sort/RadixSortExample.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// RadixSortExample.swift
3+
//
4+
//
5+
// Created by Cheer on 2017/3/2.
6+
//
7+
//
8+
9+
import Foundation
10+
11+
func radixSort1(_ arr: inout [Int]) {
12+
13+
var temp = [[Int]](repeating: [], count: 10)
14+
15+
for num in arr { temp[num % 10].append(num) }
16+
17+
for i in 1...Int(arr.max()!.description.characters.count) {
18+
19+
for index in 0..<temp.count {
20+
21+
for num in temp[index] {
22+
temp[index].remove(at: temp[index].index(of: num)!)
23+
temp[(num / Int(pow(10, Double(i)))) % 10].append(num)
24+
}
25+
}
26+
}
27+
28+
arr = temp[0]
29+
}

0 commit comments

Comments
 (0)