feature(swift): Reimplement merge_sort and top_k (#898)

* feat: Add swift-collections

* fix: use heap

* refactor: merge

* fix: import HeapModule
pull/899/head
nuomi1 1 year ago committed by GitHub
parent ba74d4bba7
commit 7605cab160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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_capacity", targets: ["max_capacity"]),
.executable(name: "max_product_cutting", targets: ["max_product_cutting"]), .executable(name: "max_product_cutting", targets: ["max_product_cutting"]),
], ],
dependencies: [
.package(url: "https://github.com/apple/swift-collections", branch: "release/1.1"),
],
targets: [ targets: [
// helper // helper
.target(name: "utils", path: "utils"), .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"]), .executableTarget(name: "avl_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["avl_tree.swift"]),
// chapter_heap // chapter_heap
.executableTarget(name: "my_heap", dependencies: ["utils"], path: "chapter_heap", sources: ["my_heap.swift"]), .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 // chapter_graph
.executableTarget(name: "graph_adjacency_matrix", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_matrix.swift"]), .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"]), .executableTarget(name: "graph_adjacency_list", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list.swift"]),

@ -4,21 +4,22 @@
* Author: nuomi1 (nuomi1@qq.com) * Author: nuomi1 (nuomi1@qq.com)
*/ */
import HeapModule
import utils import utils
/* k */ /* k */
func topKHeap(nums: [Int], k: Int) -> [Int] { func topKHeap(nums: [Int], k: Int) -> [Int] {
// k // k
var heap = Array(nums.prefix(k)) var heap = Heap(nums.prefix(k))
// k+1 k // k+1 k
for i in stride(from: k, to: nums.count, by: 1) { for i in stride(from: k, to: nums.count, by: 1) {
// //
if nums[i] > heap.first! { if nums[i] > heap.min()! {
heap.removeFirst() _ = heap.removeMin()
heap.insert(nums[i], at: 0) heap.insert(nums[i])
} }
} }
return heap return heap.unordered
} }
@main @main

@ -5,37 +5,38 @@
*/ */
/* */ /* */
// [left, mid]
// [mid + 1, right]
func merge(nums: inout [Int], left: Int, mid: Int, right: Int) { func merge(nums: inout [Int], left: Int, mid: Int, right: Int) {
// // [left, mid], [mid+1, right]
let tmp = Array(nums[left ..< (right + 1)]) // tmp
// var tmp = Array(repeating: 0, count: right - left + 1)
let leftStart = left - left //
let leftEnd = mid - left var i = left, j = mid + 1, k = 0
// //
let rightStart = mid + 1 - left while i <= mid, j <= right {
let rightEnd = right - left if nums[i] <= nums[j] {
// i, j tmp[k] = nums[i]
var i = leftStart i += 1
var j = rightStart k += 1
// nums } else {
for k in left ... right { tmp[k] = nums[j]
// j++
if i > leftEnd {
nums[k] = tmp[j]
j += 1 j += 1
k += 1
}
} }
// <= i++ //
else if j > rightEnd || tmp[i] <= tmp[j] { while i <= mid {
nums[k] = tmp[i] tmp[k] = nums[i]
i += 1 i += 1
k += 1
} }
// > j++ while j <= right {
else { tmp[k] = nums[j]
nums[k] = tmp[j]
j += 1 j += 1
k += 1
} }
// tmp nums
for k in tmp.indices {
nums[left + k] = tmp[k]
} }
} }

Loading…
Cancel
Save