Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(swift): Reimplement merge_sort and top_k #898

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions codes/swift/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"branch" : "release/1.1",
"revision" : "4a1d92ba85027010d2c528c05576cde9a362254b"
}
}
],
"version" : 2
}
5 changes: 4 additions & 1 deletion codes/swift/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ let package = Package(
.executable(name: "max_capacity", targets: ["max_capacity"]),
.executable(name: "max_product_cutting", targets: ["max_product_cutting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-collections", branch: "release/1.1"),
],
targets: [
// helper
.target(name: "utils", path: "utils"),
Expand Down Expand Up @@ -141,7 +144,7 @@ let package = Package(
.executableTarget(name: "avl_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["avl_tree.swift"]),
// chapter_heap
.executableTarget(name: "my_heap", dependencies: ["utils"], path: "chapter_heap", sources: ["my_heap.swift"]),
.executableTarget(name: "top_k", dependencies: ["utils"], path: "chapter_heap", sources: ["top_k.swift"]),
.executableTarget(name: "top_k", dependencies: ["utils", .product(name: "HeapModule", package: "swift-collections")], path: "chapter_heap", sources: ["top_k.swift"]),
// chapter_graph
.executableTarget(name: "graph_adjacency_matrix", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_matrix.swift"]),
.executableTarget(name: "graph_adjacency_list", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list.swift"]),
Expand Down
11 changes: 6 additions & 5 deletions codes/swift/chapter_heap/top_k.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
* Author: nuomi1 (nuomi1@qq.com)
*/

import HeapModule
import utils

/* 基于堆查找数组中最大的 k 个元素 */
func topKHeap(nums: [Int], k: Int) -> [Int] {
// 将数组的前 k 个元素入堆
var heap = Array(nums.prefix(k))
var heap = Heap(nums.prefix(k))
// 从第 k+1 个元素开始,保持堆的长度为 k
for i in stride(from: k, to: nums.count, by: 1) {
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
if nums[i] > heap.first! {
heap.removeFirst()
heap.insert(nums[i], at: 0)
if nums[i] > heap.min()! {
_ = heap.removeMin()
heap.insert(nums[i])
}
}
return heap
return heap.unordered
}

@main
Expand Down
55 changes: 28 additions & 27 deletions codes/swift/chapter_sorting/merge_sort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,39 @@
*/

/* 合并左子数组和右子数组 */
// 左子数组区间 [left, mid]
// 右子数组区间 [mid + 1, right]
func merge(nums: inout [Int], left: Int, mid: Int, right: Int) {
// 初始化辅助数组
let tmp = Array(nums[left ..< (right + 1)])
// 左子数组的起始索引和结束索引
let leftStart = left - left
let leftEnd = mid - left
// 右子数组的起始索引和结束索引
let rightStart = mid + 1 - left
let rightEnd = right - left
// i, j 分别指向左子数组、右子数组的首元素
var i = leftStart
var j = rightStart
// 通过覆盖原数组 nums 来合并左子数组和右子数组
for k in left ... right {
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
if i > leftEnd {
nums[k] = tmp[j]
j += 1
}
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
else if j > rightEnd || tmp[i] <= tmp[j] {
nums[k] = tmp[i]
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
// 创建一个临时数组 tmp ,用于存放合并后的结果
var tmp = Array(repeating: 0, count: right - left + 1)
// 初始化左子数组和右子数组的起始索引
var i = left, j = mid + 1, k = 0
// 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中
while i <= mid, j <= right {
if nums[i] <= nums[j] {
tmp[k] = nums[i]
i += 1
}
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
else {
nums[k] = tmp[j]
k += 1
} else {
tmp[k] = nums[j]
j += 1
k += 1
}
}
// 将左子数组和右子数组的剩余元素复制到临时数组中
while i <= mid {
tmp[k] = nums[i]
i += 1
k += 1
}
while j <= right {
tmp[k] = nums[j]
j += 1
k += 1
}
// 将临时数组 tmp 中的元素复制回原数组 nums 的对应区间
for k in tmp.indices {
nums[left + k] = tmp[k]
}
}

/* 归并排序 */
Expand Down