您的位置:首页 > 其它

简单的算法---冒泡排序

2015-07-07 11:23 225 查看
刚开始学习算法,写了个小小的Demo,如有不好的地方欢迎大家多多指教!

NSMutableArray *array = [
NSMutableArray arrayWithObjects:@"12",@"12",@"99",@"76",@"76",
nil];

for (
int i = 4; i >=
0; i --) {
for (
int j = 0; j < i ; j ++ ) {
int a = [[ array
objectAtIndex: j ] intValue];
int b = [[array
objectAtIndex: (j + 1)]
intValue ] ;
int temp =
0;

if (a <= b) {
temp = b ;
b = a ;
a = temp ;

[ array replaceObjectAtIndex:j
withObject:[ NSNumber
numberWithInt:a]] ;
[ array replaceObjectAtIndex:(j +
1) withObject:[
NSNumber numberWithInt:b]] ;
}
}
}

for (int i =
0; i < array.count; i ++) {
NSLog(@"%@", [array
objectAtIndex:i]) ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: