C language fast sorting, extending the recursive wisdom from bubble sorting algorithm

Keywords: Programming

In fact, there are many sorting algorithms faster than fast sorting, and there are many algorithms with more advantages, but in many interviews written examination, the most is still fast sorting. It is the easiest to understand and implement the O(nlogn) time sorting algorithm (note that the words used here are not necessarily easy to understand, for example, the algorithm unfamiliar with tree structure may not be easy to implement heap sorting, while fast sorting does not introduce a new data structure).

(suppose we need to sort from left to right from small to large) fast sorting is based on a key point. Select a benchmark, and put the smaller one on the left and the larger one on the right. Then the left and right two groups that have been allocated will choose a benchmark respectively, and then realize their respective grouping of "put the smaller one on the left and the larger one on the right", so as to recursively reciprocate, and finally the most fine-grained ones will be arranged in order, and the whole array will be arranged in order.

The method is simple, but how to effectively realize the function of "put the left side smaller than the benchmark and put the right side larger than the benchmark" is very particular:

Create two arrays of the same length, a array is smaller than the benchmark, b array is larger than the benchmark? And then traverse? So it needs twice as much storage space.

Fast sorting is realized by "putting the left side smaller than the benchmark, and the right side larger than the benchmark". It adopts the method of "transposing left and right, reducing both sides". Transposition is realized only in the original array operation, without additional array storage space and bubbling

The code is as follows:

void quick_sort(int* nums, int left, int right){
    int init_right = right;
    int init_left = left;

    bool from_right_search = true;
    int i_m = left;
    int middle = nums[i_m];
    left += 1;
    while(left <= right){
        if(from_right_search){
            if(nums[right] <= middle){
                int tmp = nums[right];
                nums[right] = nums[i_m];
                nums[i_m] = tmp;
                from_right_search = false;
                i_m = right;
            }else{
                right -= 1;
            }
        }else{
            if(nums[left] > middle){
                int tmp = nums[left];
                nums[left] = nums[i_m];
                nums[i_m] = tmp;
                from_right_search = true;
                i_m = left;
            }else{
                left += 1;
            }
        }
    }
    // printf("%d, %d, %d---", init_left, i_m, init_right);
    if(init_left < i_m - 1){
        if(init_left + 1 == i_m - 1){
            if(nums[init_left] > nums[i_m-1]){
                int tmp = nums[init_left];
                nums[init_left] = nums[i_m-1];
                nums[i_m-1] = tmp;
            }
        }else{
            quick_sort(nums, init_left, i_m-1);
        }
    }
    if(i_m+1 < init_right){
        if(i_m+2 == init_right){
            if(nums[i_m+1] > nums[init_right]){
                int tmp = nums[i_m+1];
                nums[i_m+1] = nums[init_right];
                nums[init_right] = tmp;
            }
        }else{
            quick_sort(nums, i_m+1, init_right);
        }
    }
}

Posted by raven_web on Wed, 05 Feb 2020 07:52:50 -0800