-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSout.swift
56 lines (48 loc) · 1.26 KB
/
QuickSout.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
/// quich sort
func quickSort(arr: inout [Int], start: Int, end: Int) {
guard start < end else {
return
}
let middle = getMiddle(arr: &arr, start: start, end: end)
// 2 middle left range do the same
// print(" middle is", middle, " ~ ", arr)
quickSort(arr: &arr, start: 0, end: middle-1)
// 3 middle right range do the same
quickSort(arr: &arr, start: middle+1, end: end)
}
// always get the certain middle
private func getMiddle(arr: inout [Int], start: Int, end: Int) -> Int {
let pivot = arr[end]
var left = start
var right = end - 1
while true {
// print("I", left, " compare ", right)
while(left < end && arr[left] <= pivot) {
left += 1
}
if left == end {
break
}
while(start <= right && arr[right] > pivot) {
right -= 1
}
// print("II ", left, " compare ", right)
if left < right {
let t = arr[right]
arr[right] = arr[left]
arr[left] = t
// print(arr)
}else {
// print(" xxx ", arr[left], " change ", arr[end])
let d = arr[left]
arr[left] = arr[end]
arr[end] = d
break
}
}
// print("> ", arr)
return left
}
let a = [3, 4, 56, 88, 0, 10]
quickSort(arr: &a, start: 0, end: a.count-1)
print(a)