刚开始学习算法,写了个小小的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]) ;
}