iOS面试算法之快速排序

52 阅读1分钟

`- (void)quickSortArr:(NSMutableArray *)array withLeftIndex:(NSInteger)leftIndex andRightIndex:(NSInteger )rightIndex{

if (leftIndex > rightIndex) {
    return;
}

NSInteger i = leftIndex;
NSInteger j = rightIndex;
NSInteger key = [array[i]integerValue];

while (i < j) {
    while (i < j && key <= [array[j] integerValue]) {
        j --;
    }
    array[i] = array[j];
    
    while (i < j && key >= [array[i] integerValue]) {
        i ++;
    }
    array[j] = array[i];
}
array[i] = @(key);

//前面排序
[self quickSortArr:array withLeftIndex:leftIndex andRightIndex:i -1];
//后面排序
[self quickSortArr:array withLeftIndex:i + 1 andRightIndex:rightIndex];

}`