OC 的冒泡排序

NSMutableArray *temp =[NSMutableArray arrayWithObjects:@"3",@"5",@"4",@"1",@"7", nil];

for (int i = 0; i < [temp count]; i++) {

for (int j = i+1; j < [temp count]; j++) {

int a = [[temp objectAtIndex:i]intValue];

int b = [[temp objectAtIndex:j]intValue];

if ( a > b) {

[temp replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%d",b]];

[temp replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%d",a]];

}

}

}

你可能感兴趣的:(OC 的冒泡排序)