Last active
August 29, 2015 14:06
-
-
Save alexfish/03da03d6b47eb860ba68 to your computer and use it in GitHub Desktop.
quicksort!
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
def qsort(list, f): | |
if len(list) <= 1: | |
return list | |
pivot = list.pop(0) | |
less = [] | |
more = [] | |
for x in list: | |
if f(x, pivot): | |
more.append(x) | |
else: | |
less.append(x) | |
return qsort(less, f) + [pivot] + qsort(more, f) | |
list = [1, 3, 4, 6, 1, 99, 40, 12, 500, 349, 230, 123, 34, 12, 43] | |
print qsort(list, lambda x, y: x > y) |
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
import UIKit | |
var items = [30, 23, 11, 0, 29, 99, 3] | |
func quickSort<T: Comparable>(var list: [T]) -> [T] { | |
if list.count <= 1 { | |
return list | |
} | |
let pivot = list.removeAtIndex(0) | |
return quickSort(list.filter{ return $0 <= pivot }) + [pivot] + quickSort(list.filter{ return $0 > pivot }); | |
} | |
quickSort(items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment