-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathContents.swift
44 lines (30 loc) · 1.16 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
//: [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.partitionLomuto(array: &array, lowest: lowest, highest: highest)
Array.quickSort(array: &array, lowest: lowest, highest: pivot - 1)
Array.quickSort(array: &array, lowest: pivot + 1, highest: highest)
} else {
debugPrint(#function + " lowest param is bigger than highest: ", lowest, highest)
}
}
private static func partitionLomuto(array: inout [Element], lowest: Int, highest: Int) -> Int {
let pivot = array[highest]
var i = lowest
for j in lowest..<highest {
if array[j] <= pivot {
array.swapAt(i, j)
i += 1
}
}
array.swapAt(i, highest)
return i
}
}
//: 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)