Three kinds of optimization for sorting--bubble

Source: Internet
Author: User

BubblesortBubble Sort is the first sort of contact for many people, and because of this simple approach, most people do not delve into it, so the code is written like this:
void Bubblesort (int *arr, int len) {assert (arr); int i = 0;int j = 0;int tmp = 0;for (i = 0; i < len-1; i++) {for (j = 0 ; J < len-i-1; J + +)       //Every sort of a trip, then there must be an order in the back, which can reduce the loop narrowing of the sorting range {if (arr[j]>arr[j + 1]) {tmp = Arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;} }}}



Actually, what I'm trying to tell you is bubble sort. There are also three ways to optimize: (The following are examples in ascending order)

For example, to sort the following set of data: 1 2 3 4 5 6 7 8- 9 According to the above arrangement, the first trip will be 9 and after the exchange has been ordered, so the next 8 trips are redundant. At this point we can add a marker to determine whether the data is exchanged during each trip, and if there is no data exchanged, then it is already in order.
void Bubblesort (int *arr, int len) {assert (arr); int i = 0;int J = 0;int flag = 0;int tmp = 0;for (i = 0; i < len-1; i+ +) {flag = 1;                              Flag initially 1for (j = 0; J < len-i-1, j + +)       //Each sort of a trip, then there must be an orderly behind, you can reduce the sort of the range {if (arr[j]>arr[j + 1])             //As long as you want to exchange data , flag will be modified {TMP = Arr[j];arr[j] = arr[j + 1];arr[j + 1] = Tmp;flag = 0;                    As long as this number is not fully ordered, it will be changed flag 0}}if (flag)                             //If the sort of a trip, found to be orderly, then do not enter If,flag not modified {break;}}




This modification, although improved the efficiency of the bubble sort, but is not the most ideal!!!
For example now to sort the following set of data:1 2 5 7 4 3 6 8 9

based on the algorithm above, we can write the following code:
void Bubblesort (int *arr, int len) {assert (arr); int i = 0;int J = 0;int flag = 0;int tmp = 0;int m = 0;                  Used to record the position of the last interchange int k = len-1;for (i = 0; i < len-1; i++) {m = 0;flag = 1;for (j = 0; J < K; J + +)       //unordered range only from first element To the last Exchange position k{if (Arr[j]>arr[j + 1]) {tmp = Arr[j];arr[j] = arr[j + 1];arr[j + 1] = Tmp;flag = 0;                    As long as the number is not fully ordered, the flag is bound to be modified to 0m = j;}} if (flag)                             //If the order is found to be ordered, then no entry to If,flag has not been modified {break;} k = m;                             Place K as the last swap position}}


written in this way, the efficiency of bubble sequencing has been greatly improved, let us introduce the third optimization Method!!!
Sort this group of numbers: 2 3 4 5 6 7 8 9-1
based on the algorithm above, we can write the following code:
void Bubblesort (int *arr, int len) {assert (arr); int i = 0;int J = 0;int flag = 0;int m = 0;       Record the position of the last interchange int n = 0;int k = len-1;for (i = 0; i < len-1; i++) {m = 0;flag = 1;//positive sequence scan for maximum value for (j = N; j < K; J + + )       //The range of the unordered area is only from the first element to the last exchange position of the previous trip k{if (Arr[j]>arr[j + 1]) {int tmp = Arr[j];arr[j] = arr[j + 1];arr[j + 1] = Tmp;flag = 0;                    As long as the number is not fully ordered, the flag is bound to be modified to 0m = j;}} K = m;if (flag)                    //If the order is found to be orderly, then no entry to If,flag has not been modified {break;} Reverse-order Scan find minimum for (j = k; j>n; j--)       //unordered range only from first element to last interchange position k{if (Arr[j]<arr[j-1]) {int tmp = Arr[j];arr[j] = Arr[j-1];arr[j-1] = Tmp;flag = 0;                    As long as this number is not fully ordered, it will be changed flag 0}}n++;if (flag)                    //If the sort of a trip, found to be orderly, then do not enter the If,flag was not modified {break;}                           Place K as the last swap position}}


This kind of improvement after the bubble sort is not very bad!!! ^v^

Three kinds of optimization for sorting--bubble

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.