Skip to content

Instantly share code, notes, and snippets.

@tianhonghui
Last active August 29, 2015 14:06
Show Gist options
  • Save tianhonghui/29b67026b5518001a5e5 to your computer and use it in GitHub Desktop.
Save tianhonghui/29b67026b5518001a5e5 to your computer and use it in GitHub Desktop.
quicksort Swift Recursion
// Playground - noun: a place where people can play
func quickSort <T : Comparable>(input : Array<T>)->(Array<T>){
if(input.count <= 1) {
return input
}
let pivot = input[0]
let tail = input[1...input.count-1]
let less = Array(tail.filter{$0<pivot})
let bigger = Array(tail.filter{$0>=pivot})
return quickSort(less) + [pivot] + quickSort(bigger)
}
let a = [7,3,6,2,8,10,1]
quickSort(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment