-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathQuickSort.swift
70 lines (57 loc) · 2.1 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
import Foundation
extension Array where Element: Comparable {
/// Sorts the array using the QuickSort algorithm in place.
///
/// The QuickSort algorithm sorts the array by first choosing a pivot. This pivot is used to rearrange
/// all elements, moving the smaller ones to the left of it. This operation is then recursevely applied
/// to the subarrays formed around the pivot.
mutating func quickSort() {
guard count > 1 else {
return
}
_quickSort(from: 0, to: count - 1)
}
mutating private func _quickSort(from left: Int, to right: Int) {
guard left < right, right - left > 0 else {
return
}
let pivotIndex = partition(from: left, to: right)
_quickSort(from: left, to: pivotIndex - 1)
_quickSort(from: pivotIndex + 1, to: right)
}
/// This method is where the pivot is chosen, so the smaller elements get moved to the left,
/// and the bigger ones to the right.
mutating private func partition(from left: Int, to right: Int) -> Int {
/// Chooses the pivot, which in this case is always the first element, which is not very efficient.
let pivotIndex = left
swapAt(pivotIndex, right)
let pivot = self[right]
var i = left
for j in i ..< right {
// If the element is smaller than the pivot, move it to the left.
if self[j] <= pivot {
swapAt(i, j)
i += 1
}
}
// Move the pivot to its right sorted position.
swapAt(i, right)
return i
}
/// Returns a sorted version of this array using the QuickSort algorithm.
func quickSorted() -> Array {
var copy = self
copy.quickSort()
return copy
}
}
// Use the following code to test it:
// var numbers = [1002, 42, 55, 124, 205]
// debugPrint(numbers.quickSorted())
//
// numbers.quickSort()
// debugPrint(numbers)
//
// The console should print:
// [42, 55, 124, 205, 1002]
// [42, 55, 124, 205, 1002]