Common sorting Algorithms (ii) Exchange sort (bubble sort, quick sort)

Source: Internet
Author: User
Tags prev

Today, what we bring to you is the exchange sort.

First, let's take a look at what is called swap sequencing. The so-called exchange, which is based on the comparison of the two record key values in the sequence of the position of the two records in the sequence, the characteristics of the Exchange order is: a record of large key values to the end of the sequence of movement, the key value of a small record to the front of the sequence movement.

so next, let's take a look. Bubble sort.


Bubble sort (Bubble sort)is a simpler sort algorithm in the field of computer science.

It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.


The bubbling sorting algorithm works as follows:

Compares the adjacent elements. If the first one is bigger than the second one, swap them both.

Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.

Repeat the above steps for all elements, except for the last one.

Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.


The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So bubble sort is a stable sorting algorithm.

void Bubblesort (int* a,int size)//bubble sort {for (Int. j= 0;j<size-1;j++) for (int i = 0;i<size-j-1; i++) {if (a[i]>a[i+1] ) Swap (a[i],a[i+1]);}}



Next is the more difficult to understand the quick sort.

First of all, we understand what things are quick to sort.

Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Quick sorting has recursive and non-recursive notation. First, I'll introduce you to the recursive notation.

int Mid (int* a,int left,int right) {int key = A[right];int begin = Left;int end = Right;while (begin <end) {while (Begin &L T;end && A[begin] <=key) begin++;if (begin <end) a[end--] =a[begin];while (begin <end && Amp A[end] >=key) end--;if (begin <end) a[begin++] =a[end];} A[begin] = Key;return begin;} void QuickSort (int* A,int left, int. right)//Quick Sort {if (left >= right) return;int Indix = Mid (a,left,right); QuickSort (a,left,indix-1); QuickSort (a,indix+1,right);}

The above approach is the most common quick sort of recursive notation.

Another of the following is the approach mentioned in the introduction to Algorithms , which the reader can look at

int Quicksort_or (int* a,int left, int right) {int prev = Left-1;int cur = Left;int key = A[right];for (; cur <= right; c ur++) {if (A[cur] <= key) {++prev;swap (a[prev],a[cur]);}} return prev;} void QuickSort (int* A,int left, int. right)//Quick Sort {if (left >= right) return;int Indix = Quicksort_or (a,left,right); QuickSort (a,left,indix-1); QuickSort (a,indix+1,right);}

As can be seen from the above two methods, it is important to find the value of key in the quick sort, so some optimization is proposed.

int Randpartition (int* A, int left, int. right)//three number in {assert (a); int mid = left + (right-left)/2;if (A[left]<a[mid] {if (A[mid] < a[right]) return A[mid];else if (A[left] > A[right]) return A[left];elsereturn a[right];} else//{if (a[mid]> a[right]) return A[mid];else if (A[left] > A[right]) return A[right];elsereturn a[left];}}

This can greatly reduce the key to the maximum or minimum situation, more conducive to fast sorting.

However, when the interval is very small, but also recursive, so that not only waste resources, but also make the operation speed slowed, so someone thought, when the interval is less than a certain degree, can we use other sort to replace it? Therefore, there is an optimization

void QuickSort (int* A,int left, int. right)//Quick Sort {if (right-left<13) Insersort (a,right-left); Else{if (left >= right ) Return;int Indix = Mid1 (a,left,right); QuickSort (a,left,indix-1); QuickSort (A,indix+1,right);}}

When the interval is less than 13 o'clock, you can adjust the insertion sort (if you do not know the insertion sort, please see my blog (a))




Non-recursive method

Void quicksort_no (int* a,int left,int right) {     assert (a);      stack<int> s;     s.push (left);//index of stacked array      s.push (right);//stack array with subscript     while  (!s.empty ())    {           int tmpright = s.top () ;              s.pop ();             int tmpleft = s.top ();               s.pop ();   int div = &NBSP;MID1 (a, tmpleft, tmpright);//Sort the array into the number of left intervals less than or equal to the number of intervals               if  (tmpleft < div - 1)             {                                      //two-digit subscript for the left interval of the press stack                     s.push (Tmpleft);                     s.push (div - 1);             }                       if  (div + 1 <  tmpright)           {                    s.push (div + 1);      //two-digit subscript in the right section of the press stack                      s.push (tmpright);            }   }  }

The above method is implemented by means of a stack, which is explained in detail in the comments.


If you want to visually feel the sorting algorithm http://blog.jobbole.com/11745/


If you want to have a quick sort of further understanding and deepen the classmate, you can look at http://dsqiu.iteye.com/blog/1707060


This blog post lists the Exchange sort, basically can master the summary, glimpse, superficial understanding. If you have any suggestions or criticisms and additions, please leave a message stating that you are grateful for more information please visit the Internet.

Common sorting Algorithms (ii) Exchange sort (bubble sort, 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.