///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)
网友评论