美文网首页
Insert Sort. O(n^2); Ω(n)

Insert Sort. O(n^2); Ω(n)

作者: R0b1n_L33 | 来源:发表于2017-09-19 10:50 被阅读13次
///Insert Sort. O(n^2); Ω(n)
func insertSort<T: Comparable>(_ array: inout [T]) {
    for i in 1..<array.count {
        let current = array[i]
        var target = 0
        for j in (0..<i).reversed() {
            if current < array[j] {
                array[j+1] = array[j]
            } else {
                target = j+1
                break
            }
        }
        array[target] = current
    }
}

var s = try Int.randomArray()
insertSort(&s)

相关文章

网友评论

      本文标题:Insert Sort. O(n^2); Ω(n)

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