Bubble Sort algorithm

Source: Internet
Author: User
Tags comparison continue sort

The simplified version of the bucket is not only the legacy of the previous section, but more importantly: it is a waste of space! For example, the range that needs to be sorted is between 0~2100000000, then you need to apply for 2,100,000,001 variables, which means to write int a[2100000001]. Because we need to use 2,100,000,001 "barrels" to store the number of times each number occurs between 0~2100000000. Even if you only have 5 numbers to sort (for example, these 5 numbers are 1,1912345678,2100000000,18000000 and 912345678), you still need 2,100,000,001 buckets, which is a waste of space! Also, if you need to sort now is no longer an integer, but some decimal, such as the 5.56789,2.12,1.1,3.123,4.1234 of the five number of the small size of the order to do? Now let's learn another new sort algorithm: bubble sort. It can solve these two problems very well.

The basic idea of bubble sort is to compare two adjacent elements at a time, and exchange them if they are in a wrong order.

For example, we need to sort 12 35 99 18 76 of these 5 numbers from large to small. Since it is from the big to the small sort that means the smaller the more lean, you do not think I am talking nonsense, but this sentence is very important (∩_∩).

First compare the size of the 1th and 2nd digits, now the 1th is 12, and the 2nd bit is 35. Find out that 12:35 is small, because we want to be smaller and better, so we need to exchange the position of these two digits. The order of the 5 numbers after the exchange is 35 12 99 18 76.

According to the previous method, continue to compare the size of the 2nd and 3rd digits, the 2nd digit is 12, and the 3rd bit is 99. 12:99 is small, so you need to swap the positions of these two digits. The order of the 5 numbers after the exchange is 35 99 12 18 76.

According to the rules just now, continue to compare the size of the 3rd and 4th digits, and swap positions if the 3rd bit is smaller than the 4th bit. The order of the 5 numbers after the exchange is 35 99 18 12 76.

Finally, compare the 4th and 5th digits. After 4 comparisons the order of the 5 numbers is 35 99 18 76 12.

After 4 comparisons we found that the smallest number was in place (already in the last one, please note that the number of the 12 move process), is not very magical. Now let's take a moment to recall the process of comparison. Each time you compare the two numbers adjacent to each other, and if the following number is larger than the previous number, swap the positions of the two digits. Until the last two numbers are finished, the smallest number is the last one. Just like a bubble, step by step and then "roll" until the last one. So this sort of method has a nice name for "bubble sort".

So here we are, in fact, that our order only places the smallest of the 5 digits. We call it a "trip" for each digit. Below we will continue to repeat just the process, the remaining 4 number of one by one to return.

OK now to start the second trip, the goal is to return the 2nd small number. First of all, compare the 1th and 2nd digits, if the 1th bit is smaller than the 2nd bit, swap the position. The order of the 5 numbers after the exchange is 99 35 18 76 12. Next you should be able to compare the 2nd and 3rd digits, the 3rd and 4th digits in turn. Note that there is no need to compare the 4th and 5th digits at this time. Because at the end of the first trip you can be sure that the 5th place is the smallest. The order of the 5 numbers after the end of the second trip is 99 35 76 18 12.

"Third Trip" is the same. After the third trip, the order of the 5 numbers is 99 76 35 18 12.

Now it's the last "four" trip. Some students have to ask again, this is not already lined up? Do we have to go on? Of course, it's just a coincidence, you can try it on a few other counts maybe not. Can you find examples of such data? Please give it a try.

The "bubble sort" principle is: Each trip can only be determined to return a number of digits. That the first trip can only be determined to the last number (both the 5th) back, the second can only be the last 2nd digits on the number (both 4th) back to the position, the third trip will only return the number on the 3rd digit (3rd), and now there are two positions in front of which there is no return, so we still need to proceed to the "step four".

The "Four trip" only needs to compare the size of the 1th and 2nd digits. Because the number in the back three positions, now the 1th digit is 99, 2nd digit is 76, do not need to exchange. The order of these 5 numbers remains unchanged at 99 76 35 18 12. To this sort of perfect end, 5 numbers already have 4 digits back, the last number can only be placed in the 1th place.

Finally, we sum up: if there are n number to order, just the number of n-1, that is, to carry out n-1 trip operation. and "Every trip" needs to start from 1th to the next two number of comparisons, the smaller number of the back, after the comparison, move backwards. Continue to compare the size of the following two consecutive digits, repeat this step until the last one has not yet been returned, and the number that has been returned is no longer required to be compared (the number that has been returned is what you compare, Wasteful expression).

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.