5. Quick Sort

Source: Internet
Author: User

First, the basic idea

By dividing the sorted data into separate parts, one part of the data is smaller than the other, and then the two parts of the data are sorted quickly, and the whole sorting process can be recursive to make the whole sequence orderly.

In the process of segmentation, the choice of pivot element is very important. The reasons are as follows:

(1) Two parts of the data is the hub element as the demarcation point , less than the number of the pivot element is placed on the left side of the pivot element, and the number of the pivot element is greater than the total on the right of the hub element;

(2) Generally choose the left, the right and the center of the three elements of the central value as a pivot element, that is, the three-digit method , it can largely avoid the grouping "one-sided" situation.

Two or three count in

In the process of the fast- running, we have to take an element as the pivot value, and use this number to divide the sequence into two parts.

Here we take the three-digit method, that is, take the left, middle, and right end three numbers, and then to sort, the middle number as the pivot value.

Three, according to the pivot value Division

Four, the code

/* Quick Sort */void QuickSort (int a[], int left, int. right) {if (left >= right) return;//takes a three-digit method, gets the pivot element and places it at the end of the current pending sequence Dealpiv OT (A, left, right); int pivot = a[right-1];//Pivot is a pivot element, and its subscript is right-1 int i = left, j = right-1;//set two cursors to mark the interval to be sorted//when I = j, the entire interval has been searched while (i! = j) {while (I < J && A[i] <= pivot)//Because the pivot element is on the right, so start from the left ++i;while (i < J &&A mp A[J] >= pivot)--j;if (i < j) Swap (A[i], a[j]);} At this point i = J swap (A[i], a[right-1]); sort (A, left, i-1); sort (A, i+1, right);} handle pivot element void Dealpivot (int a[], int left, int right) {int mid = (left + right)/2;if (A[left] > A[mid]) swap (A[left], a[ Mid]), if (A[left] > A[right]) swap (A[left], a[right]), if (A[mid] > A[right]) swap (A[mid], a[right]); swap (A[mid], a[ RIGHT-1]);//The pivot element is placed on the second-to-last element, so you need to retrieve it from the left first}

  

5. 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.