forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.swift
206 lines (173 loc) · 6.15 KB
/
Quicksort.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import Foundation
/*
Easy to understand but not very efficient.
*/
func quicksort<T: Comparable>(a: [T]) -> [T] {
if a.count <= 1 {
return a
} else {
let pivot = a[a.count/2]
let less = a.filter { $0 < pivot }
let equal = a.filter { $0 == pivot }
let greater = a.filter { $0 > pivot }
return quicksort(less) + equal + quicksort(greater)
}
}
// MARK: - Lomuto
/*
Lomuto's partitioning algorithm.
This is conceptually simpler than Hoare's original scheme but less efficient.
The return value is the index of the pivot element in the new array. The left
partition is [low...p-1]; the right partition is [p+1...high], where p is the
return value.
The left partition includes all values smaller than or equal to the pivot, so
if the pivot value occurs more than once, its duplicates will be found in the
left partition.
*/
func partitionLomuto<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
// We always use the highest item as the pivot.
let pivot = a[high]
// This loop partitions the array into four (possibly empty) regions:
// [low ... i] contains all values <= pivot,
// [i+1 ... j-1] contains all values > pivot,
// [j ... high-1] are values we haven't looked at yet,
// [high ] is the pivot value.
var i = low
for j in low..<high {
if a[j] <= pivot {
(a[i], a[j]) = (a[j], a[i])
i += 1
}
}
// Swap the pivot element with the first element that is greater than
// the pivot. Now the pivot sits between the <= and > regions and the
// array is properly partitioned.
(a[i], a[high]) = (a[high], a[i])
return i
}
/*
Recursive, in-place version that uses Lomuto's partioning scheme.
*/
func quicksortLomuto<T: Comparable>(inout a: [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)
}
}
// MARK: - Hoare partitioning
/*
Hoare's partitioning scheme.
The return value is NOT necessarily the index of the pivot element in the
new array. Instead, the array is partitioned into [low...p] and [p+1...high],
where p is the return value. The pivot value is placed somewhere inside one
of the two partitions, but the algorithm doesn't tell you which one or where.
If the pivot value occurs more than once, then some instances may appear in
the left partition and others may appear in the right partition.
Hoare scheme is more efficient than Lomuto's partition scheme; it performs
fewer swaps.
*/
func partitionHoare<T: Comparable>(inout a: [T], low: Int, high: Int) -> Int {
let pivot = a[low]
var i = low - 1
var j = high + 1
while true {
repeat { j -= 1 } while a[j] > pivot
repeat { i += 1 } while a[i] < pivot
if i < j {
swap(&a[i], &a[j])
} else {
return j
}
}
}
/*
Recursive, in-place version that uses Hoare's partioning scheme. Because of
the choice of pivot, this performs badly if the array is already sorted.
*/
func quicksortHoare<T: Comparable>(inout a: [T], low: Int, high: Int) {
if low < high {
let p = partitionHoare(&a, low: low, high: high)
quicksortHoare(&a, low: low, high: p)
quicksortHoare(&a, low: p + 1, high: high)
}
}
// MARK: - Randomized sort
/* Returns a random integer in the range min...max, inclusive. */
public func random(min min: Int, max: Int) -> Int {
assert(min < max)
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
/*
Uses a random pivot index. On average, this results in a well-balanced split
of the input array.
*/
func quicksortRandom<T: Comparable>(inout a: [T], low: Int, high: Int) {
if low < high {
// Create a random pivot index in the range [low...high].
let pivotIndex = random(min: low, max: high)
// Because the Lomuto scheme expects a[high] to be the pivot entry, swap
// a[pivotIndex] with a[high] to put the pivot element at the end.
(a[pivotIndex], a[high]) = (a[high], a[pivotIndex])
let p = partitionLomuto(&a, low: low, high: high)
quicksortRandom(&a, low: low, high: p - 1)
quicksortRandom(&a, low: p + 1, high: high)
}
}
// MARK: - Dutch national flag partitioning
/*
Swift's swap() doesn't like it if the items you're trying to swap refer to
the same memory location. This little wrapper simply ignores such swaps.
*/
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
if i != j {
swap(&a[i], &a[j])
}
}
/*
Dutch national flag partitioning
Partitions the array into three sections: all element smaller than the pivot,
all elements equal to the pivot, and all larger elements.
This makes for a more efficient Quicksort if the array contains many duplicate
elements.
Returns a tuple with the start and end index of the middle area. For example,
on [0,1,2,3,3,3,4,5] it returns (3, 5). Note: These indices are relative to 0,
not to "low"!
The number of occurrences of the pivot is: result.1 - result.0 + 1
Time complexity is O(n), space complexity is O(1).
*/
func partitionDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int, pivotIndex: Int) -> (Int, Int) {
let pivot = a[pivotIndex]
var smaller = low
var equal = low
var larger = high
// This loop partitions the array into four (possibly empty) regions:
// [low ...smaller-1] contains all values < pivot,
// [smaller... equal-1] contains all values == pivot,
// [equal ... larger] contains all values > pivot,
// [larger ... high] are values we haven't looked at yet.
while equal <= larger {
if a[equal] < pivot {
swap(&a, smaller, equal)
smaller += 1
equal += 1
} else if a[equal] == pivot {
equal += 1
} else {
swap(&a, equal, larger)
larger -= 1
}
}
return (smaller, larger)
}
/*
Uses Dutch national flag partitioning and a random pivot index.
*/
func quicksortDutchFlag<T: Comparable>(inout a: [T], low: Int, high: Int) {
if low < high {
let pivotIndex = random(min: low, max: high)
let (p, q) = partitionDutchFlag(&a, low: low, high: high, pivotIndex: pivotIndex)
quicksortDutchFlag(&a, low: low, high: p - 1)
quicksortDutchFlag(&a, low: q + 1, high: high)
}
}