Last active
August 29, 2015 14:06
-
-
Save tianhonghui/29b67026b5518001a5e5 to your computer and use it in GitHub Desktop.
quicksort Swift Recursion
This file contains hidden or 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
// 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