swift实现排序算法

前端之家收集整理的这篇文章主要介绍了swift实现排序算法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift实现排序算法


swift插入排序
func insertionSort() {
var x,y,key : Int
for (x = 0 ;x < numberList . count ;x++) {
key =
numberList [x]
for (y = x;y > - 1 ;y--) {
if (key < numberList [y]) {
numberList . removeAtIndex (y + 1 )
insert (key,atIndex: y)
}
}
}
}

冒泡排序
func bubbleSort() {
Int
for x in 0 ..< numberList . count {
passes = (
count - 1 ) - x;
for y 0 ..<passes {
key =
numberList [y]
if (key > numberList [y + 1 ]) {
z =
1 ]
1 ] = key
numberList [y] = z
}
}
}
}

猜你在找的Swift相关文章