冒泡排序(Objective-C --- NSMutableArray类目版)

- (void)bubbleSort
{
  for (int i = 0; i < [self count] - 1; i++) {
    for (int j = 0; j < [self count] - i - 1; j++) {
      if (NSOrderedDescending == [[self objectAtIndex:j] compare:[self objectAtIndex:j + 1]]) {
        [self exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
      }
    }
  }
}

注:本方法写在可变数组NSMutable的类目中,直接给self发消息就可以对数组进行操作


你可能感兴趣的:(Objective-C)