ios 冒泡排序

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *numbers = [NSMutableArray arrayWithObjects:@"17",@"28",@"36",@"15",@"39", nil];
    NSLog(@"排序前%@",numbers);
    for (int i = 0; i < 5 - 1; i++) {
        //比较的躺数
        for (int j = 0; j < 5 - 1 - i; j++) {
            //比较的次数
            if ([numbers[j] intValue] > [numbers[j + 1] intValue]) {
                //这里为升序排序
                
                int temp = [numbers[j] intValue];
                numbers[j] = numbers[j + 1];
                //OC中的数组只能存储对象,所以这里转换成string对象
                numbers[j + 1] = [NSString stringWithFormat:@"%d",temp];
            }
        }
    }
    NSLog(@"排序后%@",numbers);
}

 

posted @ 2018-07-26 11:47  狼狼a够  阅读(176)  评论(0编辑  收藏  举报