Algorithm and data structure-sort (2) bubble sort (medium)

Source: Internet
Author: User

Previous bubbleAlgorithmIs from the first position of the array, compare the number of each position with the remaining number, if there is a smaller (or larger, the following uses a small one as an example. By analyzing this process, we can know that the result of this algorithm is to put the smallest one in all the numbers at the beginning, and the order of other numbers is disordered. The second time, we extract the second small number to the beginning. After each sorting, the subsequent sorting is not helpful. We will think, is there a sort algorithm that can also move the second or third smallest order to the front when we mention the smallest one? In this way, the number of exchanges decreases during the second sorting. The following Bubble sorting method is used. The result of the previous sorting can be helpful for the next sorting. The main idea is as follows:

1. start from the first number and start from left to right to compare the two adjacent numbers. If a number is smaller than the previous one, the positions of the two adjacent numbers are exchanged. Otherwise, there is no need to exchange. After the first cycle is completed, we will find that the maximum number of 9 is at the end, and the second is at the bottom of the second. (If the preceding sorting method is used, this result will not be obtained );

2. Compare the adjacent two numbers from the first number and left to right. If the number is smaller than the previous one, the position is switched. But pay attention to the second comparison, only use to compare to the second position of the array. Because the maximum number in the array has been placed in the last bit of the array through the first comparison, you do not need to compare it with the last bit during the second comparison.

3. Similarly, compare the number from the first to the last to the third ....... Until the end, we only need to compare the first and second numbers.

The following is an example of the first comparison and the second comparison.













The specific implementation is as follows:Code:

PublicList <Int> Bublesort (list <Int> Sortlist)


For(IntI = 0; I <sortlist. Count; I ++)


// Method 1: place the largest value at the end of the array.

For(IntJ = 0; j <sortlist. Count-I-1; j ++)


If(Sortlist [J]> sortlist [J + 1])


IntTemp = sortlist [J];

Sortlist [J] = sortlist [J + 1];

Sortlist [J + 1] = temp;




// Method 2: place the maximum number in the header of the array.

// For (Int J = sortlist. Count-2; j> = I; j --)


// If (sortlist [J]> sortlist [J + 1])


// Int temp = sortlist [J];

// Sortlist [J] = sortlist [J + 1];

// Sortlist [J + 1] = temp;







According to the above ideas, it should be easy to understand this code. Here, the second for loop can be written in two ways: one is to move the maximum number to the end of each operation, and the other is to move the maximum number to the front of each operation.

is there any improvement on this sort algorithm? If our sorting sequence is an ordered sequence, you will find that the number of comparisons will be as large as that of other sequences. Is this redundant, you need to know that it takes more time to compare the data. If you process a large amount of data, this will affect the efficiency. Let's think about it and see if there are any other improvement methods?

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: 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.