-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortAlgorithm.swift
More file actions
230 lines (215 loc) · 6.78 KB
/
SortAlgorithm.swift
File metadata and controls
230 lines (215 loc) · 6.78 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Algorithm.swift
// Meniny Lab
//
// Blog : https://meniny.cn
// Github: https://github.com/Meniny
//
// No more shall we pray for peace
// Never ever ask them why
// No more shall we stop their visions
// Of selfdestructing genocide
// Dogs on leads march to war
// Go ahead end it all...
//
// Blow up the world
// The final silence
// Blow up the world
// I don't give a damn!
//
// Screams of terror, panic spreads
// Bombs are raining from the sky
// Bodies burning, all is dead
// There's no place left to hide
// Dogs on leads march to war
// Go ahead end it all...
//
// Blow up the world
// The final silence
// Blow up the world
// I don't give a damn!
//
// (A voice was heard from the battle field)
//
// "Couldn't care less for a last goodbye
// For as I die, so do all my enemies
// There's no tomorrow, and no more today
// So let us all fade away..."
//
// Upon this ball of dirt we lived
// Darkened clouds now to dwell
// Wasted years of man's creation
// The final silence now can tell
// Dogs on leads march to war
// Go ahead end it all...
//
// Blow up the world
// The final silence
// Blow up the world
// I don't give a damn!
//
// When I wrote this code, only I and God knew what it was.
// Now, only God knows!
//
// So if you're done trying 'optimize' this routine (and failed),
// please increment the following counter
// as a warning to the next guy:
//
// total_hours_wasted_here = 0
//
// Created by Elias Abel on 2017/1/5.
// Copyright © 2017 MobiMagic. All rights reserved.
//
public extension Array where Element: Comparable {
/// Sort Compare closure
public typealias SortingCompareClosure = (Element, Element) -> Bool
}
public extension Array where Element: Comparable {
// MARK: - 冒泡排序
/// Bubble Sort
///
/// - Parameter compare: Compare closure
/// - Returns: Sorted Array
public func bubbleSort(by compare: Array.SortingCompareClosure) -> [Element] {
guard self.count > 1 else {
return self
}
var items = self
let count = items.count
for i in 0..<count-1 {
var j = count - 1
while j > i {
if !compare(items[j-1], items[j]) {
(items[j-1], items[j]) = (items[j], items[j-1])
}
j-=1
}
}
return items
}
/// Bubble Sort, automatically compared by `l < r`
///
/// - Returns: Sorted Array
public func bubbleSortAutomatically() -> [Element] {
return self.bubbleSort(by: { (l, r) -> Bool in
return l < r
})
}
// MARK: - 插入排序
/// Insertion Sort
///
/// - Parameter compare: Compare closure
/// - Returns: Sorted Array
public func insertionSort(by compare: Array.SortingCompareClosure) -> [Element] {
guard self.count > 1 else {
return self
}
var items = self
let count = items.count
for i in 1..<count {
let key = items[i]
var j = i - 1
while j >= 0 && !compare(items[j], key) {
items[j+1] = items[j]
j-=1
}
items[j+1] = key
}
return items
}
/// Insertion Sort, automatically compared by `l < r`
///
/// - Returns: Sorted Array
public func insertionSortAutomatically() -> [Element] {
return self.insertionSort(by: { (l, r) -> Bool in
return l < r
})
}
// MARK: - 归并排序
/// Merge Sort
///
/// - Parameter compare: Compare closure
/// - Returns: Sorted Array
public func mergeSort(by compare: Array.SortingCompareClosure) -> [Element] {
func internal_merge(left: [Element], right: [Element], by compare: Array.SortingCompareClosure) -> [Element] {
var result = [Element]()
var l = 0, r = 0
while l < left.count || r < right.count {
if r == right.count || (l < left.count && compare(left[l], right[r])) {
result.append(left[l])
l+=1
} else {
result.append(right[r])
r+=1
}
}
return result
}
var items = self
if items.count < 2 {
return items
} else if items.count == 2 {
if !compare(items[0], items[1]) {
(items[0], items[1]) = (items[1], items[0])
}
return items
} else {
let middle = items.count/2
let left = Array(items[0..<middle]).mergeSort(by: compare)
let right = Array(items[middle..<items.count]).mergeSort(by: compare)
return internal_merge(left: left, right: right, by: compare)
}
}
/// Merge Sort, automatically compared by `l < r`
///
/// - Returns: Sorted Array
public func mergeSortAutomatically() -> [Element] {
return self.mergeSort(by: { (l, r) -> Bool in
return l < r
})
}
// MARK: - 快速排序
/// Quick Sort
///
/// - Parameter compare: Compare closure
/// - Returns: Sorted Array
public func quickSort(by compare: Array.SortingCompareClosure) -> [Element] {
guard self.count > 1 else {
return self
}
var items = self
func internal_partition(_ items: inout [Element], left: Int, right: Int, by compare: Array.SortingCompareClosure) -> Int {
let random = left + Int(arc4random_uniform(UInt32(right-left)))
let key = items[random]
(items[left], items[random]) = (items[random], items[left])
var j = left
for i in (left+1)...right {
if compare(items[i], key) {
j+=1
if i != j {
(items[i], items[j]) = (items[j], items[i])
}
}
}
(items[left], items[j]) = (items[j], items[left])
return j
}
func internal_quickSort(_ items: inout [Element], left: Int, right: Int, by compare: Array.SortingCompareClosure) {
if left < right {
let middle = internal_partition(&items, left: left, right: right, by: compare)
internal_quickSort(&items, left: left, right: middle-1, by: compare)
internal_quickSort(&items, left: middle+1, right: right, by: compare)
}
}
internal_quickSort(&items, left: 0, right: items.count-1, by: compare)
return items
}
/// Quick Sort, automatically compared by `l < r`
///
/// - Returns: Sorted Array
public func quickSortAutomatically() -> [Element] {
return self.quickSort(by: { (l, r) -> Bool in
return l < r
})
}
}