[Swift Algorithm] Quick sort

作者: sunlitamo | 来源:发表于2016-07-13 14:00 被阅读70次
    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 [] }
    }
    
    

    相关文章

      网友评论

        本文标题:[Swift Algorithm] Quick sort

        本文链接:https://www.haomeiwen.com/subject/fuwsjttx.html