Deep Quick Sorting

Keywords: Python less

Deep Quick Sorting

sort sorting is familiar to many Acmer s and is often used, so how does it work?

Visual Quick Sorting Algorithms

[External Link Picture Transfer Failure (img-3EUGGwyL-1565315336951) https://cdn.jsdelivr.net/gh/306714577/resources@1.05/images/algorithmic profiles/quick sorting.gif)]

principle

Select any number in the sequence (usually the first number), and then divide the sequence into two subsequences larger than this number and smaller than this number. Repeat the above steps to complete the sorting

For convenience, hereinafter referred to as a sequence element smaller than the reference value is a small element, and a sequence element larger than the reference value is a large element.

Give an example

Method 1

Choose a[0] as the benchmark. Ideally, there are large elements behind it, not exchanged.
As you can see, the exchange strategy is: small elements appear later
Specific realization:

  1. Baseline pivot = a[0] for determining the sequence to be sorted
  2. The remaining sequence is divided by traversal. By comparing and exchanging, the left half is small and the right half is large.
  3. Finally, by exchanging the benchmark and the last bit of small element, a quick sequence arrangement can be completed.
  4. Namely
    • Beginning - > Benchmark - Residual Sequence
    • -> Benchmark - Small Element - Large Element
    • -> Small Element-Benchmark-Large Element``

Quick sorting of the original array a[5]= {4,5,7,3,6} in the first round

beg = 0;
end = 4;

Take pivot = a[beg] = a[0]
Set index = beg + 1

Next, traverse [beg+1,end], which is the element in [1, 4].
According to the strategy, whenever a [i] < pivot occurs
Exchange values of index and a[i] to ensure that small values are changed to the front
And index ++, keep looking for the next commutative little element
If it is not found, do not deal with it.

By the end of the traversal of the [beg+1,end] interval, the small and large elements have been divided, and the pivot and the last small element have been exchanged.
Complete a round of quick sorting

pivot = a[0] = 4
index = 1
4 5 7 3 6
 Ergodic [1,4] interval
 // Find 3 less than 4, swap a[i] and index
4 3 7 5 6
index = 2   //index++
43.7.56//End of traversal not found again
 // At this point, it is determined that the subscript of the last small element is index - 1 = 1
 index = 1 //pivot and its exchange
 // Complete a round of quick sorting when the exchange is complete
3 4 7 5 6

Method 2

Select a[0] as pivot, set index left and right
Specify the starting position of the remaining sequence after the pivot is removed by left and right, respectively.
The main idea and method are the same, but there are slight differences in writing due to two cursors.

The final situation:
    The left side of the index left is full of `small elements'.`
    The right side of the index right is full of `big elements'.`

Exchange strategy: index left points to large elements, index right points to small elements. Exchange is performed when both occur simultaneously. If only one party reaches the exchange condition, then continue to loop until the other party also reaches the exchange condition.

4 5 7 3 6
pivot = a[0] = 4
left = 1,right = 4
4 5 7 3 6
 Ergodic [1,4] interval
 Left = 1 - > 5 > 4, large elements appear in left, satisfying exchange conditions
 Right = 3 - > 3 < 4, right appears small elements, satisfying exchange conditions
 Execute the exchange and get
4 3 7 5 6
 At this point, left + + after left = 2, right -- after right = 2
 While (left < right) satisfies, continue the cycle
 > If you exit the loop at left == right, you will be uncertain whether the values of left and right point to values larger than pivot, resulting in unnecessary errors after swapping (of course, you can also swap (right - 1) values, and a train of thought from the previous method)

Continue the loop: right -- after right = 1,left + + after left = 3
 Then swap the values of pivot and right (0,1)
Get a quick sequence
3 4 7 5 6

code implementation

C++ Code Implementation

#include <iostream>
#include <vector>
using namespace std;

int quick_method_1(vector<int> &v,int beg,int end){
    int pivot = v[beg];
    int index = beg + 1;
    for(int i = index;i <= end;++i){
        if(v[i] <= pivot){
            swap(v[i],v[index++]);
        }
    }
    swap(v[beg],v[index-1]);
    return index - 1;
}
int quick_method_2(vector<int> &v,int beg,int end){
    int pivot = v[beg];
    int left = beg + 1;
    int right = end;
    while(left <= right){
        if(v[left] < pivot) left++;
        else if(v[right] > pivot) right--;
        else if(v[left] >= pivot && v[right] <= pivot){
            swap(v[left++],v[right--]);
        }
    }
    swap(v[beg],v[right]);
    return right;
}
void quick_sort(vector<int> &v,int beg,int end){
    if(beg < end){
        int pivot = quick_method_1(v,beg,end);
        quick_sort(v,beg,pivot-1);
        quick_sort(v,pivot+1,end);
    }
};
int main()
{
    vector<int> v;
    v.push_back(5);
    v.push_back(2);
    v.push_back(9);
    v.push_back(4);
    quick_sort(v,0,3);
    for(int i = 0;i < 4;i++){
        cout << "v[" << i << "] = " << v[i] << endl;
    }
    return 0;
}

Python Code Implementation

def quick_method_1(seq,beg,end):
    pivot = seq[beg]    # Pivot
    index = beg + 1
    for i in range(beg+1,end+1):    # The Starting Position of Residual Sequences
        if(seq[i] <= pivot):
            seq[i],seq[index] = seq[index],seq[i]
            index += 1  # Python does not have + + and -
    seq[beg],seq[index-1] = seq[index-1],seq[beg]
    return index - 1
def quick_method_2(seq,beg,end):
    pivot  = seq[beg]
    left = beg + 1  # Left Index
    right = end # Right Index
    while(left <= right):   # Attention equals
        if(seq[left] < pivot):
            left += 1
        elif(seq[right] > pivot):
            right -= 1
        elif(seq[left] >= pivot and seq[right] <= pivot):
            seq[left],seq[right] = seq[right],seq[left]
            left += 1
            right -= 1
    seq[beg],seq[right] = seq[right],seq[beg]
    return right
def quick_sort(seq,beg,end):
    if(beg < end):
        # Method 1
        # pivot = quick_method_1(seq,beg,end)
        # Method 2
        pivot = quick_method_2(seq,beg,end) 
        quick_sort(seq,beg,pivot-1) # Left Sequence of Quick Row pivot
        quick_sort(seq,pivot+1,end) # Right Sequence of Quick Row pivot
if __name__ == "__main__":
    seq = [4,44,58,63,11,34,75,5,30,6]
    quick_sort(seq,0,9)
    print(seq)

Posted by elhelaly1999 on Thu, 08 Aug 2019 19:02:58 -0700