Swap sort: bubble sort

Source: Internet
Author: User

Exchange Sort : 22 The key code for the sorted record, in reverse order, is exchanged until there is no reverse order.

The simplest sort of swap is: bubble sort.

Bubble Sort (Bubble sort, also known as bubble sort ): Continuously compare adjacent records, if not meet the ordering requirements, then exchange.

In exchange, it can be used backwards. can also be from the back forward. Look at a pre-backward sort procedure:

Original Sequence 12 3 45 33 6

Subscript 0 1 2 3 4

First trip:

3 12 45 33 6 (3,12 Exchange)

3 12 45 33 6 (12,45 without Exchange)

3 12 33 45 6 (45,33 Exchange)

3 12 33 6 45 (45,6 Exchange)

Second trip:

3 12 33 6 45 (3,12 without Exchange)

3 12 33 6 45 (12,33 without Exchange)

3 12 6 33 45 (33,6 Exchange)

Third trip:

3 12 6 33 45 (3,12 without Exchange)

3 6 12 33 45 (12,6 Exchange)

Four trips:

3 6 12 33 45 (3,6 without Exchange)

End. The above process is very detailed, I believe you must understand.

Code One:

void BubbleSort10 (int a[], int n)//  left-to-right {if (a && n > 1) {int i,j;for (i = 1; i < n; i++)     //at most only n -1-trip sort  for (j = 0; J < N-i; J + +) {if (A[j] > A[j+1]) Swap (A[j], a[j+1]);}} void BubbleSort11 (int a[], int n)  //right-to-left {if (a && n > 1) {int i,j;for (i = 1; i < n; i++) for (j = n-1 ; j>=i; j--) {if (a[j-1] > A[j]) Swap (a[j-1], a[j]);}}}


Keep thinking: If there is no element exchange in a certain order, does it mean that everything is in order?

Yes! That being the case, the next sequence will not be done.

For code one. Give the optimized code two:

void BubbleSort20 (int a[], int n)  //left-to-right {if (a && n > 1) {int i,j = N-1;bool flag = true;while (flag) {flag = False;for (i = 0; i < J; i++) if (A[i] > A[i+1]) {Swap (A[i], a[i+1]); flag = true;} j--; }}}void BubbleSort21 (int a[], int n)  //right-to-left {if (a && n > 1) {int i,j = 1;bool flag = true;while (flag) {flag = False;for (i = n-1; I >= J; i--) if (a[i-1] > A[i]) {Swap (a[i-1], a[i]); flag = true;} j + +; }}}

think again: the furthest position of the next sort to the right (or left) is simply minus one? Can it be more efficient?

Some. Record the maximum distance of the interchange element when the previous trip is sorted, and the next one is the furthest away from this position.

For code two. Give the optimized code three:

void BubbleSort30 (int a[], int n)  //left-to-right {if (a && n > 1) {int i,k,j = N-1;bool flag = true;while (flag) {flag = False;k = 0;for (i = 0; i < J; i++) {if (A[i] > A[i+1]) {Swap (A[i], a[i+1]); flag = True;k = i;}} if (k = = 0) break;j = k; }}}void BubbleSort31 (int a[], int n)   //right-to-left {if (a && n > 1) {int i,k,j = 1;bool flag = true;while (flag) {FL AG = False;k = n-1;for (i = n-1; I >= J; i--) {if (a[i-1] > A[i]) {Swap (a[i-1], a[i]); flag = True;k = i;}} if (k = = n-1) break;j = k; }} }

The code for the interchange method is this:

void Swap (int &a, int &b) {if (a!=b) {a^=b;b^=a;a^=b;}}

Test Walk ...


Summary:

The bubbling sort is stable. But not efficient. The complexity of Time is O (n^2).

Reprint please indicate the source. This address: http://blog.csdn.net/zhangxiangdavaid/article/details/30271613


If help, the top one Oh!


Column folder See here:

    • Data structure and algorithm folders
    • C pointer


Swap sort: bubble 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.