美文网首页iOS 开发技巧iOS Developer
数组排序 冒泡排序 选择排序 插入排序 快速排序

数组排序 冒泡排序 选择排序 插入排序 快速排序

作者: 喵喵嘟噜啡 | 来源:发表于2017-07-07 13:12 被阅读34次

冒泡排序

- (void) bubbleSortWithArray:(NSMutableArray *)array{
    for (int i = 0; i < array.count; i++) {
    
        for (int j = 0; j < array.count - i - 1; j++) {
            
            if ([array[j] integerValue] > [array[j+1] integerValue]) {
                
                NSString *temp = array[j];
                
                array[j] = array[j + 1];
                
                array[j + 1] = temp;
                
            }
        }
    }
}

选择排序

- (void) selectionSortWithArray:(NSMutableArray *)array{
    
    
    for (int i = 0; i < array.count - 1 ; i++) {
        //  假设 array[i] 就是最小的数
        NSString *minVal = array[i];
        //  记录我认为的最小数下标
        int minIndex = i;
        
        for (int j = i+1; j < array.count; j++) {
            // 说明我们认为的最小值,不是最小
            if ([minVal integerValue] > [array[j] integerValue]) {
                minVal = array[j];
                minIndex = j;
            }
        }
      // 最后交换
        NSString *temp = array[i];
        array[i] = array[minIndex];
        array[minIndex] = temp;
        
        
    }
}

插入排序

- (void) insertionSortWithArray:(NSMutableArray *)array{
    
    for (int i = 0; i < array.count; i++) {
        // insertVal准备插入的数
        NSString *insertVal = array[i];
        // 准备先和 insertIndex 比较
        int insertIndex = i - 1;
        
        // 如果这个条件满足,我们就好没有找到适当的位置
        while (insertIndex >= 0 && [insertVal integerValue] < [array[insertIndex] integerValue]) {
            //把数后移
            array[insertIndex + 1] = array[insertIndex];
            insertIndex--;
        }
        // 插入 这是就给 inserVal 找到适当的位置
        array[insertIndex+1] = insertVal;
    }
    
}

快速排序

- (void)quickSortArray:(NSMutableArray *)array withLeftIndex:(NSInteger)leftIndex andRightIndex:(NSInteger)rightIndex{
    
    if (leftIndex >= rightIndex) {
     //如果数组长度为0或1时返回
        return ;
    }
    
    NSInteger i = leftIndex;
    NSInteger j = rightIndex;
    //记录比较基准数
    NSInteger key = [array[i] integerValue];
    
    while (i < j) {
        /**** 首先从右边j开始查找比基准数小的值 ***/
        while (i < j && [array[j] integerValue] >= key) {
           //如果比基准数大,继续查找
            j--;
        }
        //如果比基准数小,则将查找到的小值调换到i的位置
        array[i] = array[j];
        
        /**** 当在右边查找到一个比基准数小的值时,就从i开始往后找比基准数大的值 ***/
        while (i < j && [array[i] integerValue] <= key) {
            //如果比基准数小,继续查找
            i++;
        }
        //如果比基准数大,则将查找到的大值调换到j的位置
        array[j] = array[i];
        
    }
    
    //将基准数放到正确位置
    array[i] = @(key);
    
    /**** 递归排序 ***/
    //排序基准数左边的
    [self quickSortArray:array withLeftIndex:leftIndex andRightIndex:i - 1];
    //排序基准数右边的
    [self quickSortArray:array withLeftIndex:i + 1 andRightIndex:rightIndex];
}

系统方法

- (void)sortArray:(NSArray *)array{
    NSArray *sheng = [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return NSOrderedDescending;
        }else if ([obj1 integerValue] > [obj2 integerValue]){
            return NSOrderedAscending;
        }else{
            return NSOrderedSame;
        }
    }];
}

相关文章

网友评论

    本文标题:数组排序 冒泡排序 选择排序 插入排序 快速排序

    本文链接:https://www.haomeiwen.com/subject/wlblhxtx.html