通过OC实现简单的冒泡排序

 1 NSMutableArray *arr = [@[@"0",@"3",@"2",@"6",@"5",@"4",@"7",@"1"] mutableCopy];
 2     
 3     for (int i=0; i<[arr count]-1; i++) {
 4         for (int j=0; j<[arr count]-1-i; j++) {
 5             NSString *s1 = arr[j];
 6             NSString *s2 = arr[j+1];
 7             
 8             NSInteger i1 = [s1 integerValue];
 9             NSInteger i2 = [s2 integerValue];
10             if (i1 > i2) {
11                 [arr replaceObjectAtIndex:j withObject:s2];
12                 [arr replaceObjectAtIndex:j+1 withObject:s1];
13             }
14         }
15     }
16     NSLog(@"排序后的结果:%@", [arr componentsJoinedByString:@","]);

posted @ 2016-05-23 11:51  zbblogs  阅读(347)  评论(0编辑  收藏  举报