两种语言的冒泡排序
//=============C语言的冒泡排序
inta[10] = {0,8,2,3,4,6,5,7,1,9};
//arc4random() % (b -a + 1) +a随机数
intcount =10;
for(inti =0; i < count -1; i++) {
for(intj =0; j < count -1- i; j++) {
if(a[j] > a[j +1] ) {
inttemp ;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(inti =0; i < count ; i++) {
printf("%d ",a[i]);
}
//=============OC的冒泡排序(可变数组的exchangeObjectAtIndex:方法)
NSMutableArray*a =[NSMutableArray arrayWithObjects:@"0",@"11",@"2",@"13",@"4",@"6",@"5",@"7",@"8",@"9",nil];
for(inti =0; i < [a count] -1; i++) {
for(intj =0; j < [a count] -1- i; j++) {
if([[a objectAtIndex:j] intValue] > [[a objectAtIndex:(j+1)] intValue] )//字符串转换int比较大小
{
[a exchangeObjectAtIndex:j withObjectAtIndex:(j+1)];
}
}
}
NSLog(@"%@",a);
for(idobjina) {
NSLog(@"%@",obj);
}
[a sortUsingSelector:@selector(compare:)];//compare比较不是按照字符的数值大小而是比较的字母的顺序
NSLog(@"%@",a);