Quick sorting of common sorting algorithms

Source: Internet
Author: User

The fast sort worst-case run is O (N2), but in practice it is usually the best practical choice for sorting because of its average performance: O (NLGN) is expected, and the constant factor implied in O (NLGN) notation is small.

Quick Sort is an in-place sort. At the same time, it is an unstable sort. This article mainly refers to the introduction of algorithms.

The quick sort mainly utilizes the thought of divide and conquer. It is generally divided into three steps:

Decomposition: Array a[p. R] is divided into two sub-arrays of a[p. Q-1] and A[Q+1..R], where a[p. Q-1] elements are less than or equal to A[Q],A[Q+1..R] are greater than or equal to a[q].

Workaround: Fast Sort by recursive call, A[p for sub-array. Q-1] and A[Q+1..R] sort.

Merge: Since two sub-arrays are sorted in place, merging them does not require additional action, the entire array is already ordered.

The following is a quick-sort C-language code:

1 intQuickSort (int*a,intPintR)2 {3     if(p<R)4     {5         intQ;6q=Partition (a,p,r);7QuickSort (a,p,q-1);8QuickSort (a,q+1, R);9     }Ten}

The key to fast sorting is the partition process, which pairs the array a[p. R] for in-place sorting, the code is as follows:

1 intPartition (int*a,intPintR)2 {3     inti,j,x;4x=A[p]; Select the first element as the primary5I=p;6      for(j=p;j<r;j++)7     {8         if(x>=a[j+1])//If it is less than or equal to the main element, the interchange element j+1 and I+19         {Ten          inttemp; Onetemp=a[i+1]; Aa[i+1]=a[j+1]; -a[j+1]=temp; -i++;  the         } -     } -     inttemp; In the final step, the main element is placed in the appropriate position because the first element of the selected principal is here, and I points to the last element less than or equal to the principal, so the exchange of the principal and I elements,
Make a[p. Q-1] elements are less than or equal to A[Q],A[Q+1..R] are greater than or equal to A[q] -temp=A[p]; +a[p]=A[i]; -a[i]=temp; + returni; A}

The left describes the partition process of the fast row, and the main element in the diagram selects the last one that is not the same as the code above, but it doesn't matter. On the right is a quick-sort dynamic presentation.

Always remember that I is the dividing line of less than and greater than the main element, and that the two J is the processed and unhandled dividing line, that is, the element in front of I is less than or equal to the principal, and I+1 to J are elements greater than or equal to the principal element, and the element after J is an element that has not yet been compared.

Finally, stick to the complete code I wrote in devc++:

1#include <iostream>2#include <stdlib.h>3 using namespacestd;4 5 intQuickSort (int*a,intPintR);6 intPartition (int*a,intPintR);7 8 intMain ()9 {Ten     intI,n; Onecout<<"Please input the size of your array"<<Endl; ACin>>N; -     int*a =New int[n]; -cout<<"Please input your array"<<Endl; the      for(i=0; i<n;i++) -     { -Cin>>A[i];  -     } +QuickSort (A,0, N-1); -      for(i=0; i<n;i++) +     { Acout<<a[i]<<" "; at            -     } -cout<<Endl; - Delete A; -System"Pause"); -     return 0; in } -  to intQuickSort (int*a,intPintR) + { -     if(p<R) the     { *         intQ; $q=Partition (a,p,r);Panax NotoginsengQuickSort (a,p,q-1); -QuickSort (a,q+1, R); the     } + } A  the intPartition (int*a,intPintR) + { -     inti,j,x; $x=A[p]; $I=p; -      for(j=p;j<r;j++) -     { the         if(x>a[j+1]) -         {Wuyi          inttemp; thetemp=a[i+1]; -a[i+1]=a[j+1]; Wua[j+1]=temp; -i++;  About         } $     } -     inttemp; -temp=A[p]; -a[p]=A[i]; Aa[i]=temp; +     returni; the}

In addition, there is a typical implementation of the fast-track, is the hall of the fast, Wikipedia, described in the fast sort is the implementation. Here's a quick line on Wikipedia:

1 voidSwapint*x,int*y) {2     intt = *x;3*x = *y;4*y =T;5 }6 voidQuick_sort_recursive (intArr[],intStartintend) {7     if(Start >=end)8         return;//This is to prevent the stack from being declared9     intMID =Arr[end];Ten     intleft = start, right = end-1; One      while(Left <Right ) { A          while(Arr[left] < mid && left <Right ) -left++; -          while(Arr[right] >= mid && left <Right ) theright--; -Swap (&arr[left), &arr[right]); -     } -     if(Arr[left] >=Arr[end]) +Swap (&arr[left), &arr[end]); -     Else +left++; AQuick_sort_recursive (arr, start, left-1); atQuick_sort_recursive (arr, left +1, end); - } - voidQuick_sort (intArr[],intLen) { -Quick_sort_recursive (arr,0, Len-1); -}
View Code

Quick sorting of common sorting algorithms

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.