mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:26:30 +08:00
feature(swift): Reimplement merge_sort and top_k (#898)
* feat: Add swift-collections * fix: use heap * refactor: merge * fix: import HeapModule
This commit is contained in:
parent
ba74d4bba7
commit
7605cab160
4 changed files with 52 additions and 33 deletions
14
codes/swift/Package.resolved
Normal file
14
codes/swift/Package.resolved
Normal file
|
@ -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
|
||||
}
|
|
@ -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"),
|
||||
|
@ -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"]),
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
|
|
Loading…
Reference in a new issue