冒泡排序
2018-05-08 本文已影响235人
程序员不务正业
Python:
array = [9,2,4,5,6,3,8,1]
for i in range(len(array))[::-1]:
for j in range(i):
if array[j] > array[j+1]:
array[j], array[j+1] = array[j+1], array[j]
print(array)
OC:
NSMutableArray *list = [NSMutableArray arrayWithObjects:@2,@4,@6,@3,@9,@7,@11,@10, nil];
for (int i = 0; i < list.count - 1; i++) {
for (int j = 0; j < list.count - 1 - i ; j++) {
if (list[j] > list[j+1]) {
int temp = [list[j] intValue];
list[j] = list[j+1];
list[j+1] = [NSNumber numberWithInt:temp];
}
}
}
NSLog(@"%@", list);