Sorting algorithm--Quick sort

Source: Internet
Author: User

Fast sequencing is particularly fast, mainly due to the very refined and highly optimized internal loops.

Like merge sort, fast sorting is also a recursive algorithm for splitting. The basic algorithm for array s ordering consists of the following simple four parts:

1. If the number of elements in S is 0 or 1, the return

2. Take any element of S in V, called pivot (pivot element, principal element, Datum)

3. Divide s-{v} into two not-to-be-submitted collections: S1={x∈s-{v} | x <= v} and S2={x∈s-{v} | x > v}

4. After returning {QuickSort (S1), follow V, then QuickSort (S2)

In order to complete the above step 3, there are two common methods, can be abbreviated as: One after the last two pointers, one left and one right two pointers. These two methods are only the difference in implementation, the efficiency is the same.

In fact, the main effect of fast sorting performance is the selection of pivot.

    1. One of the most common selection methods (choosing the first or last pivot) is wrong, because it has a large potential to affect the performance of fast sorting.
    2. A safe approach is to select pivot randomly.
    3. One of the best methods is called the three-digit median segmentation method (Median-of-three partitioning). is to use the median value of the array as pivot, which is obviously hard to figure out and slows down the sorting significantly. So the usual approach is to select the middle value of the three elements on the leftmost, right, and center positions as pivot.

The code is as follows:

#include <iostream> #include <random> #include <utility> #include <ctime> #include < Algorithm>using namespace Std;void QuickSort (int* arr, size_t first, size_t last); size_t Partition (int* arr, size_t fir        St, size_t last); One last two pointers size_t partition_2 (int* arr, size_t first, size_t last);//left one right two pointers int main () {const int arrsize = 10;int* arr = new Int[arrsize];static Default_random_engine e (time (0)%), static uniform_int_distribution<int> u (0, 100); /random number range [0, 100]for (size_t i = 0; i < arrsize; ++i) {Arr[i] = U (e);} QuickSort (arr, 0, ArrSize-1), cout << boolalpha;cout << is_sorted (arr, arr + arrsize) << Endl;return 0; }void QuickSort (int* arr, size_t first, size_t last) {if (first < last) {size_t pivotindex = Partition (arr, First, last ); QuickSort (arr, first, pivotIndex-1); QuickSort (arr, Pivotindex + 1, last);}}    size_t Partition (int* arr, size_t first, size_t last) {int value = arr[first];//is the primary element size_t i = firstly;Post-pointer size_t j = first + 1; The front pointer while (J <= last) {if (Arr[j] <= value) {++i;swap (Arr[i], arr[j]);} ++j;} Swap (Arr[first], arr[i]); return i;} size_t partition_2 (int* arr, size_t first, size_t last) {int value = arr[first];size_t i = first;//left pointer//**1size_t j = LA    Qty Right pointer while (I < J) {while (Arr[i] <= value) {++i;} while (Arr[j] > value) {--j;} if (I < j) {Swap (Arr[i], arr[j]);}}  Swap (Arr[first], arr[j]); **2return J;}

Here, to reduce complexity, I select the first element as pivot.

Two ways of dividing, one after the other two pointers, good understanding, I is the posterior pointer, j is the former pointer. The elements I refer to and the previous elements are <=pivot, J each +1, then judge Arr[j] and pivot relationship.

One left and one right two pointers, I think there are two places that are hard to understand (both of which have been marked in the code).

**1, select the first element in the current range as pivot, why do I (left hand) start with a?

Answer: Quick sort is recursive, recursive termination condition,! (First < last), so only the number of elements in the current range is >=2, the sorting algorithm will continue to execute.

With only two elements, there are only three cases of the size relationship between these two elements. When the first element is greater than the second element, it is advisable to set the first element to 10, the index to 0 in the array, the second element to 0, and the index to 1 in the array.

If the left pointer is first+1 (here is 1), then the subsequent while loop is not executed, so step 3 is not satisfied and the sort fails.

**2, why {swap (Arr[first], arr[j]); return j;}, instead of {swap (Arr[first], arr[i]); return i;}

The end condition of the Answer:while loop is! (I < J), after the end of the cycle, I after J, I refers to the element is, the first element greater than Pivot, J refers to the element is the last one less than equal to the pivot element. So the execution is, {swap (Arr[first], arr[j]); return J;}.

Reference: "Data structure and algorithm analysis--c language description"

Sorting algorithm--Quick sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.