Skip to content

Instantly share code, notes, and snippets.

@cyx
Forked from fjcaetano/gist:b0c00a889dc2a17efad9
Created June 5, 2014 03:33
Show Gist options
  • Save cyx/5652bcb500921fe3264f to your computer and use it in GitHub Desktop.
Save cyx/5652bcb500921fe3264f to your computer and use it in GitHub Desktop.
var randomNumbers = [42, 12, 88, 62, 63, 56, 1, 77, 88, 97, 97, 20, 45, 91, 62, 2, 15, 31, 59, 5]
func partition(v: Int[], left: Int, right: Int) -> Int {
var i = left
for j in (left + 1)..(right + 1) {
if v[j] < v[left] {
i += 1
(v[i], v[j]) = (v[j], v[i])
}
}
(v[i], v[left]) = (v[left], v[i])
return i
}
func quicksort(v: Int[], left: Int, right: Int) {
if right > left {
let pivotIndex = partition(v, left, right)
quicksort(v, left, pivotIndex - 1)
quicksort(v, pivotIndex + 1, right)
}
}
quicksort(randomNumbers, 0, randomNumbers.count-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment