iphone 冒泡排序

来源:互联网 发布:淘宝开店上传宝贝教程 编辑:程序博客网 时间:2024/04/28 15:21

转载自

http://www.devdiv.com/home.php?mod=space&uid=92724&do=blog&id=5793


NSMutableArray *p = [[NSMutableArray allocinitWithObjects:@"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]);

}

   }

}