//=============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);