[Swift 3.0] Fundamental & Algorithm

[Swift Algorithm] Quick sort

2016-07-13  本文已影响70人  sunlitamo
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 [] }
}

上一篇下一篇

猜你喜欢

热点阅读