import Foundation
func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
let pivot = a[high]
var i = low
for j in low..<high {
if a[j] <= pivot {
(a[i], a[j]) = (a[j], a[i])
i += 1
}
}
(a[i], a[high]) = (a[high], a[i])
return i
}
func quickSortLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
if low < high {
let p = partitionLomuto(&a, low: low, high: high)
quickSortLomuto(&a, low: low, high: p - 1)
quickSortLomuto(&a, low: p + 1, high: high)
}
}
var list2 = [ 10, 0, 3, 9, 2, 14, 26, 27, 1, 5, 8, -1, 8 ]
quickSortLomuto(&list2, low: 0, high: list2.count - 1)
print(list2)
经典Swift快速排序算法
最新推荐文章于 2020-08-17 14:21:28 发布