转载自
http://www.devdiv.com/home.php?mod=space&uid=92724&do=blog&id=5793
NSMutableArray *p = [[NSMutableArray alloc] initWithObjects:@"3",@"5",@"4",@"1",nil];
for (int i = 0; i<[p count]; i++)
{
for (int j=i+1; j<[p count]; j++)
{
int a = [[p objectAtIndex:i] intValue];
NSLog(@"a = %d",a);
int b = [[p objectAtIndex:j] intValue];
NSLog(@"b = %d",b);
NSLog(@"------");
if (a > b)
{
[p replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%d",b]];
[p replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%d",a]];
}
for (int i = 0; i<[p count]; i++)
{
NSLog(@"%@",[p objectAtIndex:i]);
}
}
}
本文提供了一个使用 Objective-C 对 NSMutableArray 进行排序的例子。通过双重 for 循环遍历数组并比较元素大小,实现了基本的冒泡排序算法。
618

被折叠的 条评论
为什么被折叠?



