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)
}
}
}
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
}
}
}
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
}
}
}
}