首页 > 移动平台 > 详细

iOS(OC)中的冒泡排序

时间:2016-08-12 21:15:16      阅读:268      评论:0      收藏:0      [点我收藏+]
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil];
        NSInteger count = [array count];
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count - i - 1; j++) {
               // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) {   //这里在用[array objectAtIndex:j]时候必须intValue
//                if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){  //这里整体必须有一个返回值,-1,0,1,因为compare的返回值NSComparisonResult是一个枚举类型的值,所以要返回一个值
                 
                if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == 1){  //同上potions  NSNumericSearch = 64,
                    [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];  //这里可以用exchangeObjectAtIndex:方法来交换两个位置的数组元素。
                }
            }
        }
        for (NSString *i in array) {
            NSLog(@"%@", i);
        }
         
         
         
        NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"12",@"84", @"35", @"70", @"85", @"99", nil];
        [array1 sortUsingSelector:@selector(compare:)];
        NSLog(@"%@", array);
         

 

iOS(OC)中的冒泡排序

原文:http://www.cnblogs.com/ios988/p/5766201.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!