C ++ _ Quick Sort

Quick sort: one of the ten algorithms of the twentieth century!

The basic sort of fast

Quick sort it is based on the exchange of efficient sorting algorithm, which uses a divide and conquer thought. Proceed as follows:

  1. Selecting a number as a reference number (pivot, Pivot) from the series
  2. The array is divided (the Partition), a large number of elements than the reference pivot to the right, that is smaller than the reference number of elements pivots to the left.
  3. Two right and left sections of the second step of the division operation carried out separately, each subinterval know only one element.

Fast row the most important step is to divide the (Partition). The process of dividing say is "digging" and "fill the hole" in simple language.

Small picture, please forgive

Division (Partition)


Code:

int partition(int arr[], int left, int right)  //找基准数进行划分
{
    int low = left + 1; //区间下端 
    int high = right;   //区间上端 
    int pivot = left;   //枢轴下标 
    int cur = pivot+1;  //存储位置下标 

    for(int i=low; i<=high; i++)    //从下端到上端的遍历
    {
        if(arr[i] < arr[pivot])
        {
            swap(arr[i], arr[cur]); //交换现在的值和存储位置的值 
            cur++;  //将存储位置变为下一位 
        }
    }
    swap(arr[pivot], arr[cur-1]);   //将枢轴移至数组正确位置
    return cur-1;   //返回枢轴位置 
}

void quick_sort(int arr[], int left, int right) 
{
    if (left >= right)
        return;
    int mid = partition(arr, left, right);  //获取枢轴位置 
    quick_sort(arr, left, mid - 1);     //下端区间递归 
    quick_sort(arr, mid + 1, right);    //上端区间递归 
}

Time complexity analysis

Fast discharge time in the worst case complexity is O (N²) , the average time complexity is O (n-n-log) .

How to understand it? it's actually really easy. There is assumed that the number of columns in the number n, a traversal time complexity is O (n), needs to traverse at least log (n + 1) times, up to n times.

+ Q1: Why is the least log (n + 1) times?
+ A1: Quick sort is using divide and conquer method to traverse, it can be seen as a binary tree, the number of times it is the depth of the binary tree traversal. According to the definition of the binary tree, its depth is at least log (n + 1). Thus the number of traverse quicksort is at least log (n + 1) times.

+ Q2: Why are most n traversal?
+ A2: This should be relatively easy to understand. The quick sort is still seen as a binary tree, the maximum depth of a binary tree is n. So the number of rapid traverse sort of at most n times.


Quick sort of stability

Quick sort is unstable algorithm, it does not meet the definition of a stable sorting algorithm. Algorithm Stability: assumed that there a [i] = a [j ] in the series of numbers, prior to when the sorting, the a [i] in a [j] in front, and the ordering, a [i] is still in a [j] in front of , then this sort is stable!


That's all!Thank you for watching !

Quick Sort optimization has been updated! Portal

Guess you like

Origin www.cnblogs.com/Hello-World-Blog/p/12318017.html