Why I decided to use quick sorting later?

Source: Internet
Author: User

Sorting seems very simple, but there is actually a lot of content. We all play cards, and every time we touch the cards into the cards, we are already sorting. Every time we touch a card, we place it in proper order of size, before the card we touch is inserted into its hands for the first time. Therefore, the cards in our hands are always sorted every time we finish playing the cards. This method is so simple that I am afraid of getting jokes here. Fortunately, there are only 54 cards, not cards. Otherwise, try sorting like this.

A simple method will inevitably pay a simple price, that is, it takes too much time. To this end, there are always some people who are not satisfied with simple and time-consuming methods, and they try to find a better ranking.Algorithm. As a result, semi-insert sorting, Merge Sorting, heap sorting, fast sorting, and application are generated.

Here, I will mainly talk about Merge Sorting and quick sorting.

The so-called Merge Sorting means to break down the Sorting Problem of a large sequence into two subsequences, and then recursively sort the subsequences in the same way (until each subsequence has only one element, naturally ordered). After the sub-sequence is sorted, the results are merged.

Decomposition is very easy, and the main effort is spent on merging, that is to say, the main time of merging and sorting is spent on merging. The following figure shows a process of merging and sorting.

 

Next let's take a look at quick sorting. In fact, quick sorting uses any element as the lever point (usually the first element), and then splits the lever point to traverse the elements to be arranged. A small one is left, and a large one is right.

Then sort the left and right sides once and recursively go down until all elements are sorted.

The figure is as follows:

Text is hard to describe as to the differences between insert sorting, Merge Sorting, and fast sorting. Therefore, we useProgramFor an intuitive comparison:

The following result is a random number of N integers, which are then compared by insertion sorting, Merge Sorting, and quick sorting (in seconds. Running result:

If the time is 0, it does not mean that the running time is not required, but the time is very short. If a clock cycle is not reached, it is ignored.

From the results, we can see that when the data volume is small, the centralized sorting effect is quite good and the difference is not obvious. However, when the data volume reaches tens of thousands, tens of thousands or more, the cost-effectiveness and cost-effectiveness are clear. The sorting of 200000 pieces of data is that insertion sorting cannot be tolerated for nearly 1 minute. No matter whether you can accept it or not, I cannot accept it anyway.

Therefore, I decided to use quick sorting in the future. In particular, when the data volume is large, do not hold the insertion order, in fact, Bubble Sorting is also true. Therefore, fast sorting is indeed fast. Of course, the Merge Sorting is also quite good.

A poor source program is attached. I hope the disclosure is correct. Xuyongjie1128@hotmail.com

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////

 

# Include <iostream. h> # Include <Stdlib. h> # Include <Time. h> Void Insert_sort ( Int A [], Int N) //  Insert sort  {  Int  I, j, key;  For (J = 1 ; J <n; j ++ ) {Key = A [J]; I = J- 1  ;  While (I> = 0 & A [I]>Key) {A [I + 1 ] = A [I]; I -- ;} A [I + 1 ] = Key ;}}  Int Partition ( Int A [], Int M, Int N) //  Quick Sort Decomposition  {  Int X, I, j, temp; x = A [m]; I = M;  For (J = m + 1 ; J <= N; j ++ ){  If (A [J] <= X) {I = I + 1  ; Temp = A [I]; A [I] = A [J]; A [J] = Temp;} temp = A [I]; A [I] = A [m]; A [m] = Temp;  Return  I ;}  Void Quicksort ( Int A [], Int Begin, Int End) //  Quick sorting  {  Int  Q;  If (Begin < End) {q = Partition (A, begin, end); quicksort (A, begin, Q - 1  ); Quicksort (A, Q + 1  , End );}}  Int * Union ( Int A [], Int B [], Int Abegin, Int Aend, Int Bbegin, Int Bend) //  Merge  {  Int I = abegin, j = Bbegin;  Int * C = New   Int [Aend-abegin + 1 + Bend-bbegin + 1  ];  While (I <= aend & J <= Bend ){  If (A [I] <B [J]) {C [I -Abegin + J-bbegin] = A [I]; I ++ ;}  Else  {C [I -Abegin + J-bbegin] = B [J]; j ++ ;}}  If (I> Aend ){  While (J <= Bend) {C [I -Abegin + J-bbegin] = B [J]; j ++ ;}}  Else  {  While (I <= Aend) {C [I -Abegin + J-bbegin] = A [I]; I ++ ;}}  Return  C ;}  Int * Unionsort ( Int A [], Int Begin, Int End)//  Merge Sorting  {  Int Q = (begin + end )/ 2  ;  If (Begin = End ){  Int * Temp = & A [begin];  Return  Temp ;}  If (Begin < End ){  Return Union (unionsort (A, begin, q), unionsort (A, q + 1 , End ), 0 , Q-begin, 0 , End-Q- 1  );}}  Int * Createrandomarry ( Int  N ){  Int * Data = New   Int  [N];  Int  I; For (I = 0 ; I <n; I ++ ) {Data [I] = Rand ();}  Return  Data ;}  Void Main () //  Main Function  {  Int  I, n; time_t tstart, tend;  While ( True  ) {Cout < "  \ N: \ n  "  ; CIN > N;  If (N = 0  ){  Break  ;}  Int * Data = Createrandomarry (N );  Int * Data2 = New   Int [N];  For (I = 0 ; I <n; I ++ ) {Data2 [I] = Data [I];}  Long Now = Clock (); insert_sort (data, n); cout < "  Insert sorting time:  " < Double (Clock ()-Now)/clocks_per_sec < Endl; now =Clock ();  Int * Result = unionsort (data2, 0 , N- 1  ); Cout < "  Merge Sorting time:  " < Double (Clock ()-Now)/clocks_per_sec < Endl; now = Clock (); quicksort (data2,  0 , N- 1 ); Cout < "  Quick sorting time:  " < Double (Clock ()-Now)/clocks_per_sec < Endl ;}} 

Xu Yongjie

January 18, 2013

 

 

 

 

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.