-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathContents.swift
47 lines (31 loc) · 1.24 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//: [Previous](@previous)
import Foundation
extension Array where Element: Comparable {
static func quickSort(array: inout [Element], lowest: Int, highest: Int) {
if lowest < highest {
let pivot = Array.partitionHoare(array: &array, lowest: lowest, highest: highest)
Array.quickSort(array: &array, lowest: lowest, highest: pivot)
Array.quickSort(array: &array, lowest: pivot + 1, highest: highest)
} else {
debugPrint(#function + " lowest param is bigger than highest: ", lowest, highest)
}
}
private static func partitionHoare(array: inout [Element], lowest: Int, highest: Int) -> Int {
let pivot = array[lowest]
var i = lowest - 1
var j = highest + 1
while true {
i += 1
while array[i] < pivot { i += 1 }
j -= 1
while array[j] > pivot {j -= 1 }
if i >= j { return j }
array.swapAt(i, j)
}
}
}
//: Usage
var nums = [1, 5, 6, 3, 2, 7, 8, 5, 8, 4, 2, 9, 0]
Array.quickSort(array: &nums, lowest: 0, highest: nums.count - 1)
print(#function + " sorted array : " , nums)
//: [Next](@next)