Skip to content

Instantly share code, notes, and snippets.

@alexfish
Last active August 29, 2015 14:06
Show Gist options
  • Save alexfish/03da03d6b47eb860ba68 to your computer and use it in GitHub Desktop.
Save alexfish/03da03d6b47eb860ba68 to your computer and use it in GitHub Desktop.
quicksort!
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)
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