冒泡和选择排序

SMutableArray * muArr =[NSMutableArray arrayWithArray:@[@(12),@(34),@(97),@(456),@(21)]];
    [self bubbleSort:muArr];
    [self selectSort:muArr];

//冒泡从大到小排序
- (void)bubbleSort:(NSMutableArray *)array{
   
    for (int i = 0; i< array.count-1; i++) {
        for (int j= 0; j < array.count - 1 - i; j++) {
            if ([array[j] intValue] < [array[j+1] intValue]) {
                [array exchangeObjectAtIndex:j withObjectAtIndex:j+1];
            }
        }
    }
    NSLog(@"%@",array);
}


//选择排序
- (void)selectSort:(NSMutableArray *)array{
    for (int i = 0; i

你可能感兴趣的:(冒泡和选择排序)