iosOC可变数组选择,冒泡排序
#pragma mark 可变数组的排序
NSMutableArray * array = [NSMutableArray arrayWithObjects: @"1",@"8",@"2",@"7",@"2",@"5",@"9",nil];
//选择
for (int i =0; i<[array count]-1; i++) {
for (int j = i+1; j<[array count]; j++) {
if ([array[i] intValue]>[array[j] intValue]) {
//交换
[array exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
NSLog(@"%@",array);
NSMutableArray * array1 =[[NSMutableArray alloc]initWithObjects:@"zhige",@"saozi",@"bge",@"xiaolong",@"xiaomo",@"xiaomi", nil];
//冒泡
for (int i =0; i<[array1 count]-1; i++) {
for (int j =0; j<[array1 count]-1-i; j++) {
if(([array1[j] compare:array1[j+1]]) == NSOrderedDescending){
//交换
[array1 exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
NSLog(@"%@",array1);