feat: add Swift codes for heap_sort article (#520)

pull/522/head
nuomi1 1 year ago committed by GitHub
parent 8bed60f0d7
commit 56a4385202
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -52,6 +52,7 @@ let package = Package(
.executable(name: "insertion_sort", targets: ["insertion_sort"]), .executable(name: "insertion_sort", targets: ["insertion_sort"]),
.executable(name: "quick_sort", targets: ["quick_sort"]), .executable(name: "quick_sort", targets: ["quick_sort"]),
.executable(name: "merge_sort", targets: ["merge_sort"]), .executable(name: "merge_sort", targets: ["merge_sort"]),
.executable(name: "heap_sort", targets: ["heap_sort"]),
.executable(name: "bucket_sort", targets: ["bucket_sort"]), .executable(name: "bucket_sort", targets: ["bucket_sort"]),
.executable(name: "counting_sort", targets: ["counting_sort"]), .executable(name: "counting_sort", targets: ["counting_sort"]),
.executable(name: "radix_sort", targets: ["radix_sort"]), .executable(name: "radix_sort", targets: ["radix_sort"]),
@ -115,6 +116,7 @@ let package = Package(
.executableTarget(name: "insertion_sort", path: "chapter_sorting", sources: ["insertion_sort.swift"]), .executableTarget(name: "insertion_sort", path: "chapter_sorting", sources: ["insertion_sort.swift"]),
.executableTarget(name: "quick_sort", path: "chapter_sorting", sources: ["quick_sort.swift"]), .executableTarget(name: "quick_sort", path: "chapter_sorting", sources: ["quick_sort.swift"]),
.executableTarget(name: "merge_sort", path: "chapter_sorting", sources: ["merge_sort.swift"]), .executableTarget(name: "merge_sort", path: "chapter_sorting", sources: ["merge_sort.swift"]),
.executableTarget(name: "heap_sort", path: "chapter_sorting", sources: ["heap_sort.swift"]),
.executableTarget(name: "bucket_sort", path: "chapter_sorting", sources: ["bucket_sort.swift"]), .executableTarget(name: "bucket_sort", path: "chapter_sorting", sources: ["bucket_sort.swift"]),
.executableTarget(name: "counting_sort", path: "chapter_sorting", sources: ["counting_sort.swift"]), .executableTarget(name: "counting_sort", path: "chapter_sorting", sources: ["counting_sort.swift"]),
.executableTarget(name: "radix_sort", path: "chapter_sorting", sources: ["radix_sort.swift"]), .executableTarget(name: "radix_sort", path: "chapter_sorting", sources: ["radix_sort.swift"]),

@ -0,0 +1,55 @@
/**
* File: heap_sort.swift
* Created Time: 2023-05-28
* Author: nuomi1 (nuomi1@qq.com)
*/
/* n i */
func siftDown(nums: inout [Int], n: Int, i: Int) {
var i = i
while true {
// i, l, r ma
let l = 2 * i + 1
let r = 2 * i + 2
var ma = i
if l < n, nums[l] > nums[ma] {
ma = l
}
if r < n, nums[r] > nums[ma] {
ma = r
}
// i l, r
if ma == i {
break
}
//
nums.swapAt(i, ma)
//
i = ma
}
}
/* */
func heapSort(nums: inout [Int]) {
//
for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {
siftDown(nums: &nums, n: nums.count, i: i)
}
// n-1
for i in stride(from: nums.count - 1, to: 0, by: -1) {
//
nums.swapAt(0, i)
//
siftDown(nums: &nums, n: i, i: 0)
}
}
@main
enum HeapSort {
/* Driver Code */
static func main() {
var nums = [4, 1, 3, 1, 5, 2]
heapSort(nums: &nums)
print("堆排序完成后 nums = \(nums)")
}
}
Loading…
Cancel
Save