This commit is contained in:
krahets
2024-03-21 04:22:07 +08:00
parent 35a07170c0
commit cfdb743939
52 changed files with 292 additions and 290 deletions

View File

@ -142,14 +142,12 @@ comments: true
/* 冒泡排序 */
func bubbleSort(nums: inout [Int]) {
// 外循环:未排序区间为 [0, i]
for i in stride(from: nums.count - 1, to: 0, by: -1) {
for i in nums.indices.dropFirst().reversed() {
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
for j in stride(from: 0, to: i, by: 1) {
for j in 0 ..< i {
if nums[j] > nums[j + 1] {
// 交换 nums[j] 与 nums[j + 1]
let tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
nums.swapAt(j, j + 1)
}
}
}
@ -404,14 +402,12 @@ comments: true
/* 冒泡排序(标志优化)*/
func bubbleSortWithFlag(nums: inout [Int]) {
// 外循环:未排序区间为 [0, i]
for i in stride(from: nums.count - 1, to: 0, by: -1) {
for i in nums.indices.dropFirst().reversed() {
var flag = false // 初始化标志位
for j in stride(from: 0, to: i, by: 1) {
for j in 0 ..< i {
if nums[j] > nums[j + 1] {
// 交换 nums[j] 与 nums[j + 1]
let tmp = nums[j]
nums[j] = nums[j + 1]
nums[j + 1] = tmp
nums.swapAt(j, j + 1)
flag = true // 记录交换元素
}
}

View File

@ -203,7 +203,7 @@ comments: true
for bucket in buckets {
for num in bucket {
nums[i] = num
nums.formIndex(after: &i)
i += 1
}
}
}

View File

@ -169,8 +169,8 @@ comments: true
}
// 3. 遍历 counter ,将各元素填入原数组 nums
var i = 0
for num in stride(from: 0, to: m + 1, by: 1) {
for _ in stride(from: 0, to: counter[num], by: 1) {
for num in 0 ..< m + 1 {
for _ in 0 ..< counter[num] {
nums[i] = num
i += 1
}
@ -573,19 +573,19 @@ $$
}
// 3. 求 counter 的前缀和,将“出现次数”转换为“尾索引”
// 即 counter[num]-1 是 num 在 res 中最后一次出现的索引
for i in stride(from: 0, to: m, by: 1) {
for i in 0 ..< m {
counter[i + 1] += counter[i]
}
// 4. 倒序遍历 nums ,将各元素填入结果数组 res
// 初始化数组 res 用于记录结果
var res = Array(repeating: 0, count: nums.count)
for i in stride(from: nums.count - 1, through: 0, by: -1) {
for i in nums.indices.reversed() {
let num = nums[i]
res[counter[num] - 1] = num // 将 num 放置到对应索引处
counter[num] -= 1 // 令前缀和自减 1 ,得到下次放置 num 的索引
}
// 使用结果数组 res 覆盖原数组 nums
for i in stride(from: 0, to: nums.count, by: 1) {
for i in nums.indices {
nums[i] = res[i]
}
}

View File

@ -306,7 +306,7 @@ comments: true
siftDown(nums: &nums, n: nums.count, i: i)
}
// 从堆中提取最大元素,循环 n-1 轮
for i in stride(from: nums.count - 1, to: 0, by: -1) {
for i in nums.indices.dropFirst().reversed() {
// 交换根节点与最右叶节点(交换首元素与尾元素)
nums.swapAt(0, i)
// 以根节点为起点,从顶至底进行堆化

View File

@ -124,7 +124,7 @@ comments: true
/* 插入排序 */
func insertionSort(nums: inout [Int]) {
// 外循环:已排序区间为 [0, i-1]
for i in stride(from: 1, to: nums.count, by: 1) {
for i in nums.indices.dropFirst() {
let base = nums[i]
var j = i - 1
// 内循环:将 base 插入到已排序区间 [0, i-1] 中的正确位置

View File

@ -305,12 +305,11 @@ comments: true
if nums[i] <= nums[j] {
tmp[k] = nums[i]
i += 1
k += 1
} else {
tmp[k] = nums[j]
j += 1
k += 1
}
k += 1
}
// 将左子数组和右子数组的剩余元素复制到临时数组中
while i <= mid {

View File

@ -168,12 +168,7 @@ comments: true
=== "Swift"
```swift title="quick_sort.swift"
/* 元素交换 */
func swap(nums: inout [Int], i: Int, j: Int) {
let tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp
}
[class]{}-[func]{swap}
/* 哨兵划分 */
func partition(nums: inout [Int], left: Int, right: Int) -> Int {
@ -187,9 +182,9 @@ comments: true
while i < j, nums[i] <= nums[left] {
i += 1 // 从左向右找首个大于基准数的元素
}
swap(nums: &nums, i: i, j: j) // 交换这两个元素
nums.swapAt(i, j) // 交换这两个元素
}
swap(nums: &nums, i: i, j: left) // 将基准数交换至两子数组的分界线
nums.swapAt(i, left) // 将基准数交换至两子数组的分界线
return i // 返回基准数的索引
}
```
@ -801,12 +796,12 @@ comments: true
let m = nums[mid]
let r = nums[right]
if (l <= m && m <= r) || (r <= m && m <= l) {
return mid; // m 在 l 和 r 之间
return mid // m 在 l 和 r 之间
}
if (m <= l && l <= r) || (r <= l && l <= m) {
return left; // l 在 m 和 r 之间
return left // l 在 m 和 r 之间
}
return right;
return right
}
/* 哨兵划分(三数取中值) */
@ -814,7 +809,7 @@ comments: true
// 选取三个候选元素的中位数
let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)
// 将中位数交换至数组最左端
swap(nums: &nums, i: left, j: med)
nums.swapAt(left, med)
return partition(nums: &nums, left: left, right: right)
}
```

View File

@ -304,7 +304,6 @@ $$
func countingSortDigit(nums: inout [Int], exp: Int) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
var counter = Array(repeating: 0, count: 10)
let n = nums.count
// 统计 0~9 各数字的出现次数
for i in nums.indices {
let d = digit(num: nums[i], exp: exp) // 获取 nums[i] 第 k 位,记为 d
@ -315,8 +314,8 @@ $$
counter[i] += counter[i - 1]
}
// 倒序遍历,根据桶内统计结果,将各元素填入 res
var res = Array(repeating: 0, count: n)
for i in stride(from: n - 1, through: 0, by: -1) {
var res = Array(repeating: 0, count: nums.count)
for i in nums.indices.reversed() {
let d = digit(num: nums[i], exp: exp)
let j = counter[d] - 1 // 获取 d 在数组中的索引 j
res[j] = nums[i] // 将当前元素填入索引 j