Last active
August 29, 2015 14:15
-
-
Save nuclearace/c5dcc70adb4707e0b2fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func quicksort(inout list:[Int], var left:Int = -1, var right:Int = -1) { | |
if left == -1 && right == -1 { | |
left = 0 | |
right = list.count - 1 | |
} | |
func partition(left:Int, right:Int) -> Int { | |
var i = left | |
for j in left+1...right { | |
if list[j] < list[left] { | |
i++ | |
(list[i], list[j]) = (list[j], list[i]) | |
} | |
} | |
(list[i], list[left]) = (list[left], list[i]) | |
return i | |
} | |
if right > left { | |
let pivotIndex = partition(left, right) | |
quicksort(&list, left: left, right: pivotIndex - 1) | |
quicksort(&list, left: pivotIndex + 1, right: right) | |
} | |
} |
kongtomorrow
commented
Feb 11, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment