Sort algorithm--bubble sort

Source: Internet
Author: User

In the previous article, we have introduced two basic sorting methods: Insert sort and select sort. Today's introduction is another sort of sorting method--bubbling sort.

Thought

The bubble sort is also one of the simplest and most basic sorting methods, as described in the previous two sorting approaches. The idea of bubble sorting is simple, which is to compare the size of adjacent elements, move small forward, large back, like bubbles in the water, the smallest elements after several moves, will eventually float to the surface.

For example analysis, the following data:

2 7 4 6 9 1 First compares the last two numbers, finds 1:9 small, and then moves forward

2 7 4 6 1 9 then compare 6 and 1

2 7 4 1 6 9 Continue to move forward, then 4 and 1

2 7 1 4 6 9 7 and 1 comparison

2 1 7 4 6 9 2 and 1

1 2 7 4 6 9 At this point, the first bubbling process is complete, and the smallest element 1 is moved to the first one, and no longer participates in the subsequent ordering process. The next bubbling process is the same, comparing 6 and 9, and so on, and finally getting the results.

Code

cout <<"Bubble Sort:"<< Endl;
PrintLine ( "before sort: for (int i=0; i< V.size (); i++) {
int temp = 0;
for (int j=v.size ()-1; J>0; j--) {
1] {
temp = v[j];
V[j] = V[j-1];
V[j-1] = Temp ;
}
}
}
PrintLine (after Sort: ", v);

Analysis

Since each order adds a bubble to the ordered area, after n-1 sequencing, there are n-1 bubbles in the ordered area, and the bubbles in the disordered zone are always greater than or equal to the weight of the bubbles in the ordered area, so the whole bubbling sort process needs to be n-1. The time complexity of this algorithm or O (n*n) is not an efficient algorithm.

Careful analysis is not difficult to find, the algorithm can also optimize the space, if in a certain trip to the sorting of the bubble location is not found in the exchange, then the order to sort out all the bubbles in the disordered area to meet the light in the top, the principle of the next, so the bubble sorting process can be terminated after this sequence. To do this, in the algorithm given below, introduce a Boolean exchange, which is set to false before each order begins. If an interchange occurs during the sorting process, it is set to true. Check exchange at the end of each trip, and terminate the algorithm if no swap has occurred, no longer the next order. This can reduce unnecessary comparisons. The code is as follows

int Bubble_sort (vector<Int> &v) {
cout <<"Bubble Sort:"<< Endl;
PrintLine ("Before sort:", V);
BOOL Exchange;
for (int i=0; I<v.size (); i++) {
int temp = 0;
Exchange = false;
For (int j=v.size ()-1; j>0; j--) {
if (V[j] < v[j-1]) {
temp = V[j];
V[J] = v[j-1];
v[j-1] = temp;
Exchange = true;
}
}
if (!exchange) {
Break
}
}
PrintLine ("Aftersort:", V);
}

The above is a bubble sort of a simple explanation and explanation, I hope we can be inspired and help.

Later, we will introduce some efficient sorting algorithms, so please look forward to it.

Sort algorithm--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.