常用的算法排序问题-----OC

本文介绍了Objective-C中的四种经典排序算法:冒泡排序、选择排序、插入排序和快速排序,并提供了详细的实现代码。每种排序算法都有其独特的应用场景及优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用的算法排序问题—–OC

    //OC 中常用的一些排序
    NSArray *arr = @[@"1",@"2",@"5",@"3",@"6",@"9",@"5",];

    NSMutableArray *oldArr = [NSMutableArray arrayWithArray:arr];

    NSLog(@"%@",oldArr);

    [self bubbleSort:oldArr];

    [self selectionSort:oldArr];

    [self insertSor:oldArr];

    [self quickSort:oldArr leftIndex:1 rightIndex:5];

    }

#pragma mark - bubbleSort
/**
 ①.冒泡排序算法
 @param array 参数
 */
- (void)bubbleSort:(NSMutableArray *)array {

    for (int i = 0; i < array.count; i++) {

        for (int j = 0; j < array.count-i-1; j++) {

            if ([array[j+1]integerValue] < [array[j]integerValue]) {

                int temp = [array[j] integerValue];

                array[j] = array[j+1];

                array[j+1] = [NSNumber numberWithInt:temp];
            }
        }
    }

    NSLog(@"冒泡排序后: %@",array);

}

#pragma mark - selectionSort
/**
 ②.选择排序
 @param array 参数
 */
- (void)selectionSort:(NSMutableArray *)array {

    for (int i = 0; i < array.count; i++) {

        for (int j = i+1; j <array.count ; j++) {

            if ([array[i]integerValue] > [array[j]integerValue]) {

                int temp = [array[i] integerValue];

                array[i] = array[j];

                array[j] = [NSNumber numberWithInt:temp];

            }
        }
    }

    NSLog(@"选择排序后: %@",array);

}

#pragma mark - insertSort
/**
 ③.插入排序
 @param array 参数
 */
- (void)insertSor:(NSMutableArray *)array {

    for (int i = 1; i < array.count; i++) {

        int temp = [array[i]integerValue];

        for (int j = i-1; j>=0 && temp <[array[j] integerValue]; j--) {

            array[j+1] = array[j];

            array[j] = [NSNumber numberWithInt:temp];

        }
    }

    NSLog(@"插入排序后: %@",array);

}

#pragma mark - quickSort
/**
 ④.快速排序
 @param array 参数
 */
- (void)quickSort:(NSMutableArray *)array leftIndex:(int)left rightIndex:(int)right {

    if (left < right) {

        int temp = [self getMiddleIndex:array leftIndex:left rightIndex:right];

        [self quickSort:array leftIndex:left rightIndex:temp-1];

        [self quickSort:array leftIndex:temp+1 rightIndex:right];

    }
    NSLog(@"快速排序后: %@",array);
}

- (int)getMiddleIndex:(NSMutableArray *)array leftIndex:(int)left rightIndex:(int)right {

    int tempValue = [array[left] integerValue];

    while (left < right) {

        while (left < right && tempValue <= [array[right] integerValue]) {

            right--;
        }

        if (left < right) {

            array[left]=array[right];
        }

        while (left < right && [array[left] integerValue] <= tempValue) {

            left++;
        }
        if (left < right) {

            array[left] = array[right];
        }
    }

    array[left] = [NSNumber numberWithInt:tempValue];

    return left;

}

GitHub 代码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值