# andyRon/swift-algorithm-club-cn

Switch branches/tags
Nothing to show
Latest commit ef427b5 Nov 8, 2018
 .. Failed to load latest commit information. RadixSort.playground Nov 6, 2018 image Nov 6, 2018 README.md Nov 8, 2018 README_en.md Sep 22, 2018

## 例子

• Input Array: [170, 45, 75, 90, 802, 24, 2, 66]
• Output Array (Sorted): [2, 24, 45, 66, 75, 90, 170, 802]

### 算法中的行动

• Iteration 1: 170, 90, 802, 2, 24, 45, 75, 66
• Iteration 2: 802, 2, 24, 45, 66, 170, 75, 90
• Iteration 3: 2, 24, 45, 66, 75, 90, 170, 802

## 代码

```import Foundation

// 不能处理负数
public func radixSort(_ array: inout [Int]) {
var done = false
var index: Int
var digit = 1

while !done {
done = true

var buckets: [[Int]] = []  // 我们的排序子程序是桶排序，所以让我们预定义我们的桶

buckets.append([])
}

for number in array {
index = number / digit
if done && index > 0 {
done = false
}
}

var i = 0

let bucket = buckets[j]
for number in bucket {
array[i] = number
i += 1
}
}

}
}

// 小数组的测试
var array: [Int] = [19, 4242, 2, 9, 912, 101, 55, 67, 89, 32]
print(array)

// 大数组的测试
var bigArray = [Int](repeating: 0, count: 1000)
var i = 0
while i < 100 {
bigArray[i] = Int(arc4random_uniform(1000) + 1)
i += 1
}