笔记4: 基础算法
2016-07-25 本文已影响8人
LukeHua
冒泡排序
var arr = [11,2,6,555,45,89,23,76,12,35,99,44,8,123,999]
func bubbleSort (inout arr: Array<Int> ) -> Array<Int> {
for i in 0..<arr.count {
for j in 0..<arr.count-1 - i {
if arr[j] > arr[j+1]{
let temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
}
}
}
return arr
}
bubbleSort(&arr)