swift 版 var numbers = [17, 28, 36, 15, 39]
print("排序前\(numbers)") for i in 0..<numbers.count - 1 {
for j in 0..<numbers.count - 1 - i {
if numbers[j] > numbers[j + 1] {
numbers.swapAt(j, j+1)
}
}
}
print("排序后\(numbers)")
OC版 NSMutableArray *numbers = [NSMutableArray arrayWithObjects:@"17",@"28",@"36",@"15",@"39", nil];
NSLog(@"排序前%@",numbers);
for (int i = 0; i < 5 - 1; i++) {
//比较的躺数
for (int j = 0; j < 5 - 1 - i; j++) {
//比较的次数
if ([numbers[j] intValue] > [numbers[j + 1] intValue]) {
//这里为升序排序 int temp = [numbers[j] intValue];
numbers[j] = numbers[j + 1];
//OC中的数组只能存储对象,所以这里转换成string对象
numbers[j + 1] = [NSString stringWithFormat:@"%d",temp];
}
}
}
NSLog(@"排序后%@",numbers);