iOS 常用的排序算法-OC版//快速排序 - (void)quickSort:(NSMutableArray *)arr start:(int)start end:(int)end { //

1.冒泡排序

///冒泡排序
- (void)bubbleSort:(NSMutableArray *)arr {
    //两两比较 需要比较 count - 1 次
    for (int i = 0; i < arr.count-1; i++) {
        //以下 for 循环走完后即可将最大值放到最后,此处count-1 后还需要 - i
        for (int j = 0; j < arr.count-1-i; j++) {
            // 如果 前面的数大于后面的数,就将这两个数交换位置
            if ([arr[j] intValue] > [arr[j+1] intValue]) {
                int temp = [arr[j] intValue];
                arr[j] = arr[j+1];
                arr[j+1] = [NSNumber numberWithInt:temp];
            }
        }
    }
}

NSMutableArray *arr = @[@2, @4, @9, @8, @1, @0, @3, @5, @2].mutableCopy;
NSLog(@"%@", arr);
[self bubbleSort:arr];
NSLog(@"%@", arr);

控制台输出

[2, 4, 9, 8, 1, 0, 3, 5, 2]
[0, 1, 2, 2, 3, 4, 5, 8, 9]

2.快速排序

//快速排序
- (void)quickSort:(NSMutableArray *)arr start:(int)start end:(int)end {
    //递归结束条件
    if (start >= end) {
        return;
    }
    int low = start;
    int high = end;
    int stand = [arr[start] intValue];
    while (low < high) {
        
        while (low < high && stand <= [arr[high] intValue]) {
            high--;
            self.count++;
        }
        if (low == high) {// 当key是目前最小的数时,会出现i=j的情况,
            break;
        }
        arr[low++] = arr[high];// i++ 会减少一次m[i]和key的比较
        while (low < high && stand >= [arr[low] intValue]) {
            low++;
            self.count++;
        }
        if (low == high) {// 当key是目前最大的数时(m[j]的前面),会出现i=j的情况
            break;
        }
        arr[high--] = arr[low];//j-- 会减少一次m[j]和key的比较
    }
    //把标准数赋值给低所在的位置
    arr[low] = [NSNumber numberWithInt:stand];
    //左递归
    [self quickSort:arr start:start end:low];
    //右递归
    [self quickSort:arr start:low+1 end:end];
}

3.插入排序

//插入排序
- (void)insertSort {
    NSMutableArray *array = @[@"3",@"2",@"5",@"4",@"7",@"9",@"8",@"9",@"10",@"33"].mutableCopy;
    for (int i = 1; i < array.count; i++) {
        
        int temp = [array[i] intValue];
        
        for (int j = i-1; j>=0 && temp <[array[j] intValue]; j--) {
            
            array[j+1] = array[j];
            
            array[j] = [NSNumber numberWithInt:temp];
        }
    }
    NSLog(@"插入排序后: %@",array);
}

 

4.选择排序

//选择排序
- (void)selectionSort {
    NSMutableArray *array = @[@"3",@"2",@"5",@"4",@"7",@"9",@"8",@"9",@"10",@"33"].mutableCopy;
    for (int i = 0; i < array.count; i++) {
        for (int j = i+1; j <array.count ; j++) {
            if ([array[i] intValue] > [array[j] intValue]) {
                
                int temp = [array[i] intValue];
                
                array[i] = array[j];
                
                array[j] = [NSNumber numberWithInt:temp];
            }
        }
    }
    NSLog(@"选择排序后: %@",array);
}

 

 

5.归并排序

6.双路排序

7.三路排序

8.堆排序

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值