iosOC可变数组选择,冒泡排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
以上是关于iosOC可变数组选择,冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章