extension Array {
var decompose:(head:Element,tail:[Element])?{
return count > 0 ? (self[0],Array(self[1 ..< count])) : nil
}
}
func quickSort(src:[Int])->[Int] {
if let (pivot,rest) = src.decompose {
let lesser:[Int] = rest.filter{ $0 < pivot }
let greater:[Int] = rest.filter{ $0 > pivot }
return quickSort(lesser) + [pivot] + quickSort(greater)
}
else{ return [] }
}
网友评论