forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.swift
More file actions
38 lines (33 loc) · 1003 Bytes
/
QuickSort.swift
File metadata and controls
38 lines (33 loc) · 1003 Bytes
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
//
// QuickSort.swift
// Practice
//
// Created by Greg Price on 3/31/18.
//
import Foundation
public func quick(_ ints: inout [Int]) {
divide(&ints, thisStart: 0, thisEnd: ints.count - 1)
}
fileprivate func divide(_ ints: inout [Int], thisStart: Int, thisEnd: Int) {
if thisStart < thisEnd {
let p = partition(&ints, start: thisStart, p: thisEnd)
divide(&ints, thisStart: thisStart, thisEnd: p - 1)
divide(&ints, thisStart: p + 1, thisEnd: thisEnd)
}
}
fileprivate func partition(_ ints: inout [Int], start: Int, p: Int) -> Int {
var leftPileFrontIdx = start - 1
for i in start..<p {
if ints[i] <= ints[p] {
leftPileFrontIdx += 1
swap(&ints, idx1: leftPileFrontIdx, idx2: i)
}
}
swap(&ints, idx1: leftPileFrontIdx + 1, idx2: p)
return leftPileFrontIdx + 1
}
fileprivate func swap( _ ints: inout [Int], idx1: Int, idx2: Int) {
let t = ints[idx2]
ints[idx2] = ints[idx1]
ints[idx1] = t
}