冒泡算法(实战)

//冒泡排序
- (NSMutableArray *)logArrayFunction:(NSMutableArray *)result{
//    NSMutableArray *result = [NSMutableArray arrayWithObjects:@1,@4,@2,@3,@5,nil];
    
//    for (int i = 0; i<result.count-1; i++) {
//        for (int j = 0; j<result.count-1-i; j++) {
//            NSInteger left = [result[j] integerValue];
//            NSInteger right = [result[j+1] integerValue];
//            if (left>right) {
//                [result exchangeObjectAtIndex:j withObjectAtIndex:j+1];
//            }
//        }
//    }
//
//    NSLog(@"最终结果:%@",result);//由小到大
    
    for (int i = 0; i<result.count-1; i++) {
        for (int j = 0; j<result.count-1-i; j++) {
            NSInteger left = [result[j] integerValue];
            NSInteger right = [result[j+1] integerValue];
            if (left<right) {
                [result exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }
    
    NSLog(@"最终结果:%@",result);//由大到小
    
    
    return result;
}

 

posted on 2019-02-14 15:05  高彰  阅读(223)  评论(0编辑  收藏  举报

导航