iOS排序

Crazy巴旦木發表於2018-11-08

下面是用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]];
}
複製程式碼

相關文章