mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Add the section of radix sort. (#441)
This commit is contained in:
@@ -11,50 +11,49 @@ func digit(num: Int, exp: Int) -> Int {
|
||||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
func countingSort(nums: inout [Int], exp: Int) {
|
||||
// 十进制的各位数字范围为 0~9 ,因此需要长度为 10 的桶
|
||||
var bucket = Array(repeating: 0, count: 10)
|
||||
func countingSortDigit(nums: inout [Int], exp: Int) {
|
||||
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
|
||||
var counter = Array(repeating: 0, count: 10)
|
||||
let n = nums.count
|
||||
// 借助桶来统计 0~9 各数字的出现次数
|
||||
// 统计 0~9 各数字的出现次数
|
||||
for i in nums.indices {
|
||||
let d = digit(num: nums[i], exp: exp) // 获取 nums[i] 第 k 位,记为 d
|
||||
bucket[d] += 1 // 统计数字 d 的出现次数
|
||||
counter[d] += 1 // 统计数字 d 的出现次数
|
||||
}
|
||||
// 求前缀和,将“出现个数”转换为“数组索引”
|
||||
for i in 1 ..< 10 {
|
||||
bucket[i] += bucket[i - 1]
|
||||
counter[i] += counter[i - 1]
|
||||
}
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入暂存数组 tmp
|
||||
var tmp = Array(repeating: 0, count: n)
|
||||
// 倒序遍历,根据桶内统计结果,将各元素填入 res
|
||||
var res = Array(repeating: 0, count: n)
|
||||
for i in stride(from: n - 1, through: 0, by: -1) {
|
||||
let d = digit(num: nums[i], exp: exp)
|
||||
let j = bucket[d] - 1 // 获取 d 在数组中的索引 j
|
||||
tmp[j] = nums[i] // 将当前元素填入索引 j
|
||||
bucket[d] -= 1 // 将 d 的数量减 1
|
||||
let j = counter[d] - 1 // 获取 d 在数组中的索引 j
|
||||
res[j] = nums[i] // 将当前元素填入索引 j
|
||||
counter[d] -= 1 // 将 d 的数量减 1
|
||||
}
|
||||
// 将 tmp 复制到 nums
|
||||
// 使用结果覆盖原数组 nums
|
||||
for i in nums.indices {
|
||||
nums[i] = tmp[i]
|
||||
nums[i] = res[i]
|
||||
}
|
||||
}
|
||||
|
||||
/* 基数排序 */
|
||||
func radixSort(nums: inout [Int]) {
|
||||
// 获取数组的最大元素,用于判断最大位数
|
||||
var ma = Int.min
|
||||
var m = Int.min
|
||||
for num in nums {
|
||||
if num > ma {
|
||||
ma = num
|
||||
if num > m {
|
||||
m = num
|
||||
}
|
||||
}
|
||||
// 按照从低位到高位的顺序遍历
|
||||
for exp in sequence(first: 1, next: { ma >= ($0 * 10) ? $0 * 10 : nil }) {
|
||||
// 对数组元素的第 k 位执行「计数排序」
|
||||
for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// k = 3 -> exp = 100
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSort(nums: &nums, exp: exp)
|
||||
countingSortDigit(nums: &nums, exp: exp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +61,9 @@ func radixSort(nums: inout [Int]) {
|
||||
enum RadixSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 基数排序 */
|
||||
var nums = [23, 12, 3, 4, 788, 192]
|
||||
// 基数排序
|
||||
var nums = [10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996]
|
||||
radixSort(nums: &nums)
|
||||
print("基数排序完成后 nums = \(nums)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user