iOS排序

下面是用Objective-C语言写的。

冒泡排序

-(void)bubbleSort:(NSArray <NSNumber *> *)arr{
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:arr];
    int n = (int)arr.count;
    for(int i = 0 ; i < n - 1; i++){
        for(int j = 0 ; j < n - i - 1 ; j++){
            int a = [mArr[j] intValue];
            int b = [mArr[j+1] intValue];
            if(a > b){
                int temp = a;
                [mArr replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:b]];
                [mArr replaceObjectAtIndex:j+1 withObject:[NSNumber numberWithInt:temp]];
            }
        }
    }
    NSLog(@"%@",mArr);
}
复制代码

选择排序

-(void)selectionSort:(NSArray <NSNumber *> *)arr{
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:arr];
    int n = (int)arr.count;
    for(int i = 0;i<n;i++){
        int min = i;
        for(int j = i + 1; j <n; ++j){
            int a = [mArr[j] intValue];
            int b = [mArr[min] intValue];
            if(a<b){
                min = j;
            }
        }
        if(min != i){
            int a = [mArr[i] intValue];
            int b = [mArr[min] intValue];
            int temp = a;
            [mArr replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:b]];
            [mArr replaceObjectAtIndex:min withObject:[NSNumber numberWithInt:temp]];
        }
    }
    NSLog(@"%@",mArr);
}
复制代码

插入排序

-(void)insertionSort:(NSArray <NSNumber *> *)arr{
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:arr];
    int n = (int)arr.count;
    for(int i = 1;i<n;++i){
        for(int j=i; j >0;j--){
            int a = [mArr[j] intValue];
            int b = [mArr[j-1] intValue];
            if(a<b){
                int temp = a;
                [mArr replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:b]];
                [mArr replaceObjectAtIndex:j-1 withObject:[NSNumber numberWithInt:temp]];
            }
        }
    }
    NSLog(@"%@",mArr);
}
复制代码

快速排序

-(void)quickSort:(NSArray <NSNumber *> *)arr{
    NSMutableArray *mArr = [NSMutableArray arrayWithArray:arr];
    int n = (int)arr.count;
    [self quickSort:mArr low:0 high:n-1];
    NSLog(@"%@",mArr);
}

-(void)quickSort:(NSMutableArray <NSNumber *> *)arr low:(int)low high:(int)high{
    if(low >= high){
        return;
    }
    int p = [self partition:arr low:low high:high];
    [self quickSort:arr low:low high:p-1];
    [self quickSort:arr low:p+1 high:high];
}

-(int)partition:(NSMutableArray <NSNumber *> *)arr low:(int)low high:(int)high{
    int randomIndex = rand()%(high-low+1)+low;
    [self swap:arr a:randomIndex b:low];
    
    int i = low+1;
    int j = high;
    int pivot = [arr[low] intValue];
    
    while (true) {
        while (i < high && [arr[i] intValue] < pivot) {
            i ++;
        }
        while (j > low+1 && [arr[j] intValue] > pivot) {
            j--;
        }
        if(i > j){
            break;
        }
        [self swap:arr a:i b:j];
        i++;
        j--;
    }
    [self swap:arr a:low b:j];
    return j;
}

-(void)swap:(NSMutableArray <NSNumber *> *)arr a:(int)a b:(int)b{
    int temp = [arr[a] intValue];
    [arr replaceObjectAtIndex:a withObject:arr[b]];
    [arr replaceObjectAtIndex:b withObject:[NSNumber numberWithInt:temp]];
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值