Kids Learn data structure (7): Quick Sort

Source: Internet
Author: User

Kids Learn data structure (7): Quick Sort

First, quick sort

(a) Basic ideas

Select a datum element, usually select the first element or the last element, through a scan, the waiting sequence is divided into two parts, some smaller than the Datum element, part is greater than the datum element, at this time the datum element in its correct position after the order, and then the same method recursively sorted the two parts of the division.

(ii) Example 6-1.png

Take {5, 9, 2, 7, 8, 3, 6, 1, 4, 0} as an example.
Choosing the No. 0 element 5 as the reference number, our goal of the first step is to adjust the number of smaller than 5 to the left of 5, the number of 5 to the right of 5.
(1) From left to right to observe, found 9:5 large, ready to adjust. Again from right to left to observe, found 0:5 small, ready to adjust. Swap positions of 9 and 0.
(2) Continue from left to right to observe, 2:5 small, do not tune. Go on to the right, 7:5 big, ready to adjust. Continue from right to left to spectator, 4:5 small, ready to adjust. Swap positions of 7 and 4.
(3) Continue to observe from left to right, 8:5 large, ready to adjust. Continue viewing from right to left, 1:5 small, ready to adjust. Swap positions of 8 and 1.
(4) Continue to observe from left to right, 3:5 small, do not adjust. Continue to the right to see, hit 6, ready to adjust. Continue from right to left to observe, the first encounter is 6, then from left to right or from right to left touches are 6, so 6 do not adjust, also do not need to continue to observe.
(5) Adjust the position of 3 and 5 for the last time. Got the goal of the first step, 5 smaller than the {3, 0, 2, 4, 1} are on the left of 5, than the 5 of the {6, 8, 7, 9} are on the right side of 5.
(6) The new series {3, 0, 2, 4, 1} and {6, 8, 7, 9} will continue to adjust with the above method until all the numbers have been sequenced.

Note that this is the No. 0 element as a reference number. It is also possible to use other elements as reference numbers. But for ease of understanding, it is usually either a No. 0 element or a reference number, or an intermediate element.
The following code sets the intermediate element to the number of references.

(iii) C + + code implementation
#Include<iostream>UsingNamespaceStdvoidQuickSort(int a[],int L,int R) {int i = l, j = r, TMP;int pivot = a[(L + R)/2];The value of the comparison, which is set to the middle element. It is also possible to set the first element.while (I <= j) {while (A[i] < pivot) {i++;}while (A[j] > Pivot) {j--;}if (i <= j) {tmp = A[i]; A[i] = A[j]; a[j] = tmp; i++; j--;}}if (I < R) {QuickSort (A, I, r);}if (L < j) {QuickSort (A, L, j);}}int main  () {int a[] = {6, 1, 3, 2, 4, 5}; int len = sizeof (a)/sizeof (int); QuickSort (A, 0, Len-1); for (int i = 0; i < Len; i++) {cout << a[i] << return 0;}         

Operation Result:

1 2 3
Second, "Big talk data structure" in the rapid sequencing implementation

Concrete content See "Big talk data Structure" p417-p433

#Include<iostream>UsingNamespaceStd#Define MAX 5typedefstruct{int R[max +1];int Len;} SeqIntPartition(Seq *x,int Low,int high) {int PivotKey = x->r[low]; x->r[0] = PivotKey;while (Lowwhile (Low < high && X->r[high] >= pivotkey) {high--;} X->r[low] = x->r[high];while (Low < high && X->r[low] <= pivotkey) {low++;} X->r[high] = x->r[low]; } X->r[low] = x->r[0];return low;}voidQuicksort(Seq *x,int Low,int high) {int pivot;if (Low < high) {pivot = partition (x, Low, high); Quicksort (x, Low, Pivot-1); Quicksort (x, pivot + 1, high);}} int main() {seq s; for (int i = 1; I <= MAX; i++) { cin >> s.r[i];} s.len = MAX; Quicksort (&s, 1, S.len); For (int i = 1; I <= S.len; i++) { cout << s.r[i] << ";} cout << Endl; return 0;}                  

Operation Result:

1 2 3 4 5

Note that the SWAP function has the IF judgment, the reason may refer to "the child learns the classical algorithm (13): Two number Exchange"
https://www.jianshu.com/p/7c6ef94d5059

Why fast sorting is an unstable sorting algorithm:
Take a[] = {5, 3, 3, 4, 3, 8, 9, 10} For example,
After the first round of sorting, it becomes {3, 3, 3, 4, 5, 8, 9, 10} Here the first 3, which is the original third 3.
It is not stable to see the order of the same number being chaotic.



Kids Learn data structure (7): 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.