Bubble Sorting by Sorting Algorithm

Source: Internet
Author: User

Author: hustwing hustwing@126.com MSN: hustwing@hotmail.com

Bubble Sorting is a famous sorting algorithm. When we are a beginner in sorting, we often use it to get started. When I went to college, I learned C language. The first sorting algorithm that the teacher talked about was bubble. However, its low efficiency is well known. As long as we talk about bubbling, everyone will frown, as if we use bubbling, it means that you are not considering efficiency at all. But how many people actually understand bubble?

I was not prepared to write an article specifically for bubble in algorithm, but when I realized that it took about half a day to understand it, I think it is necessary to summarize the knowledge I have learned about the bubble algorithm.

At first, I mixed up Bubble sorting and select sorting, and then came to the network for a query. I found that half of them mixed up these two algorithms like me. Below are the basic ideas of these two algorithms:

Sort by selection(Ascending)
Basic Idea:
1) For a sequence with N numbers (stored in array a (n), the minimum number is selected and the position is exchanged with the 1st number;
2) except for 1st, the smallest number is selected among the other n-1 numbers, and the position is exchanged with 2nd;
3) and so on. After N-1 is selected, the sequence is sorted in ascending order.

Bubble sort(Ascending)
Basic Idea: (compare two adjacent numbers and adjust them to the front if they are small)
1) There are n numbers (stored in array a (n). The first line is to compare each adjacent two numbers, and a small value is adjusted to the front. After the n-1 adjacent comparisons, the maximum number is already "bottom", placed in the last position, and the decimal point is "floating up ";
2) the second round of the remaining n-1 number (the maximum number has been "bottom") by the above method comparison, the number of times after the N-2 times of two adjacent comparison;
3) by analogy, N numbers are compared in a total of N-1 bits, and n-J times are compared in the J bits.

From the basic idea above, we can see that the biggest difference between the Bubble Method and the choice method is that the bubble method is an exchange of adjacent numbers, the selection rule is to compare and exchange all other numbers with the numbers at a specific position. The following differences are reflected in the Code;

--------------------------------- Simple selection and sorting -------------------------------------

Void simpleselectsort (int * const arr, int arrlength)
{
Int I, j, Imin, temp;

For (I = 0; I {
For (j = I + 1; J {
If (ARR [I]> arr [J]) // pay attention to this row. For non-adjacent comparisons, select sort.
{
Temp = arr [I];
Arr [I] = arr [J];
Arr [J] = temp;
}
}
}
}

--------------------------------- Simple Bubble Sorting -------------------------------------

Void simplebubblesort (int * const arr, int arrlength)
{
Int I, j, temp;
For (I = 0; I {
For (j = 0; j{
If (ARR [J]> arr [J + 1]) // pay attention to this line, which is an adjacent comparison.
{
Temp = arr [J];
Arr [J] = arr [J + 1];
Arr [J + 1] = temp;
}
}
}
}

Bytes ------------------------------------------------------------------------------------------

After the name is correct, let's see what other variants it has. According to the basic idea, we can imagine that if we arrange them in ascending order, we have two options: one is the two adjacent comparisons, choose to bring the smallest number to the leftmost (the frontend of the array). This is called forward bubble or forward bubble! You can also choose to take the maximum number to the rightmost, which is called Back-to-bubble or reverse bubble. Of course, smart people also think of the third method, that is, the two sides simultaneously "take", one of the largest and the smallest at a time, then a large and small (two-way Bubbling!) appears again !)! Facts prove that this is indeed a good idea, at least in terms of efficiency.

The following is the specific implementation code of these three bubble algorithms. The Code contains code related to performance comparison. Only the code in the loop section (BOLD) is the specific implementation of the bubble algorithm, at the end of the article, we will provide performance tests on these three algorithms. Of course, I have tested this data myself and it is not necessarily objective.

-------------------------------------------------------------------- Three bubble algorithms ---------------------------------------------------------------------

Sort_performance forwardbubblesort (int * const arr, int arrlength)
{
Int I, j, temp;
Bool flag = false;
Unsigned int circutimes = 0, swaptimes = 0;
Clock_t start, end;
Sort_performance sort_perform;

Start = clock ();
For (I = 0; I {
For (j = arrlength-1; j> I; j --)
{
Circutimes ++;
If (ARR [J] {
Temp = arr [J];
Arr [J] = arr [J-1];
Arr [J-1] = temp;
Flag = true;
Swaptimes ++;
}
}
If (! Flag)
{
Break;
}
}

End = clock ();
Sort_m.circutimes = circutimes;
Sort_shortm.milliseconds = end-start;
Sort_comment m.swaptimes = swaptimes;
Return sort_perform;
}
Sort_performance backwardbubblesort (int * const arr, int arrlength)
{
Int I, j, temp;
Bool flag = false;
Unsigned int circutimes = 0, swaptimes = 0;
Clock_t start, end;
Sort_performance sort_perform;
Start = clock ();
For (I = 0; I {
For (j = 0; j{
Circutimes ++;
If (ARR [J]> arr [J + 1])
{
Temp = arr [J];
Arr [J] = arr [J + 1];
Arr [J + 1] = temp;
Flag = true;
Swaptimes ++;
}
}
If (! Flag)
{
Break;
}
}
End = clock ();
Sort_m.circutimes = circutimes;
Sort_shortm.milliseconds = end-start;
Sort_comment m.swaptimes = swaptimes;
Return sort_perform;
}
Sort_performance doubledirectionbubblesort (int * const arr, int arrlength)
{
Int temp;
Int left = 0;
Int right = arrlength-1;
Int T = 0, I;
Unsigned int circutimes = 0, swaptimes = 0;
Clock_t start, end;
Sort_performance sort_perform;
Start = clock ();
Do
{
// Forward to bubble
For (I = right; I> left; I --)
{
Circutimes ++;
If (ARR [I-1]> arr [I])
{
Temp = arr [I-1];
Arr [I-1] = arr [I];
Arr [I] = temp;
T = I-1;
Swaptimes ++;
}
}
Left = t + 1;

// Then reverse bubble
For (I = left; I {
Circutimes ++;
If (ARR [I]> arr [I + 1])
{
Temp = arr [I + 1];
Arr [I + 1] = arr [I];
Arr [I] = temp;
T = I + 1;
Swaptimes ++;
}
}
Right = t-1;
} While (left
End = clock ();
Sort_m.circutimes = circutimes;
Sort_shortm.milliseconds = end-start;
Sort_comment m.swaptimes = swaptimes;
Return sort_perform;
}

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------

After testing, we can find that the performance data of these three bubble modes is as follows, and the referenced data is based on the local machine. Of course, some of them can be guessed without testing, and some of them are interesting:

1. The performance of forward and reverse bubbles is the same. I can understand this without testing.

2. the performance of Bidirectional bubbling is better than that of the first two, but this advantage is obvious only when the data volume is large (more than 5000 ), the execution time can be about 1/4 faster than the first two, but it is not as fast as I have found on the internet that there are fewer exchanges. On the contrary, the number of exchanges is the same as that of the first two types, but the number of loops is about 1/3 less than that of the first two types of bubbles. The time is obtained from the decrease in the number of loops.

3. One interesting aspect of Bidirectional bubbling is that when Data tends to be ordered, it becomes more and more efficient. The conclusions in 2 are obtained based on the random array test. As arrays become more and more ordered, the number of loops decreases rapidly, and the efficiency increases. But when the array is absolutely ordered, its number of loops is twice that of the first two! The number of exchanges is 0. I tested that when only one element in the array is unordered, it can save the number of cycles by more than 99% (the number of exchanges remains unchanged )! This can save almost 100% of the time! However, once it is absolutely ordered, its cycle times will rise to double! It seems that everything is absolutely opposite. O (∩ _ ∩) O... haha!

Article reference: http://www.zxbc.cn/html/20070425/7523.html

All the code in this article can be downloaded here, including the performance test section (bubblesort. cpp): http://download.csdn.net/source/590585

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.