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_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…
Cancel
Save