Day5 Bubble sorting, day5 bubble

Source: Internet
Author: User

Day5 Bubble sorting, day5 bubble

  Bubble Sorting: It is a basic algorithm to sort data. The sorting principle is to compare the previous one with the next one. If the previous value is large, it is exchanged. Otherwise, it is not exchanged, multiple cycles each time the maximum data is recycled to the end to complete the required.

The above figure shows the principle of Bubble sorting. The maximum value is traversed to the end of each loop, and the number of times of each loop is reduced by 1 during the loop process.

Next let's look at an instance. We will sort the list by data = [, 4. The Code is as follows:

First, retrieve the list order and the following table enumerate (

Data = [, 21, 6]

For index, I in enumerate (data [0:-1]):
If I> data [index + 1]:
Data [index + 1], data [index] = I, data [index + 1]
Print (data)
Run the following command:

[4, 10, 21, 33, 3, 54, 8, 5, 11, 2, 1, 2, 13, 6, 13]

It can be seen that the location is changed, but there is an error. 13 appears twice, and 22 disappears. This is because in the loop process, we changed the subscript of the list. After the subscript is changed, an error occurs, and some values are replaced. Because it cannot be done like this, or the following cannot be done.

Data = [, 21, 6]

For index, I in enumerate (data [0:-1]):
If I> data [index + 1]:
# Data [index + 1], data [index] = I, data [index + 1]
# Store a temporary variable to store information
Tmp = data [index + 1]
Data [index + 1] = I
Data [index] = tmp

Print (data)

In the above Code, tem is used to store a data [index + 1]. As we know, we need to exchange data [index] AND data [index + 1, one of them will be replaced, because the replaced value should be included in a new variable, so as to avoid this error.

Data = [, 21, 6]

For j in range (len (data )):
For I in range (len (data)-1 ):
If data [I]> data [I + 1]:
Tem = data [I + 1]
Data [I + 1] = data [I]
Data [I] = tem
Print (data)

The result of running the code is as follows:

[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

In the code above, our idea is to compare each element in the loop list with the previous one. If the previous one is greater than the last one, it will be replaced; otherwise, it will not move, in this way, each time we move the last maximum value to the end of the list, we can implement the function. The length of the list will be exactly the number of times, so we have added a for loop to the outside, this allows the following list to be sorted for multiple comparisons. However, the Code above can have an improvement. We know that each loop will put a maximum value at the end of the list, so the number of cycles can be decreased by one time, the code is improved as follows:

Data = [, 21, 6]

For j in range (1, len (data )):
For I in range (len (data)-j ):
If data [I]> data [I + 1]:
Tem = data [I + 1]
Data [I + 1] = data [I]
Data [I] = tem
Print (data)

The running result is as follows:

[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

The results are the same, but we have avoided a lot of unnecessary loops here. Let's take a look at the number of cycles required for the two codes. Therefore, we can define a n in it, add 1 to each loop.

Data = [, 21, 6]
N = 0
For j in range (len (data )):
# For j in range (1, len (data )):
For I in range (len (data)-1 ):
# For I in range (len (data)-j ):
N = n + 1
If data [I]> data [I + 1]:
Tem = data [I + 1]
Data [I + 1] = data [I]
Data [I] = tem
Print (n)
Print (data)

After the tests of the above two methods, the methods without improvement run 210 times, and the improved method value ran 105 times. It can be seen that a little improvement can save a lot of time.

Next let's take a look at the results of each code run:

[4, 10, 33, 21, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 54, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 54, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 54, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 54, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 54, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 54, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 54, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 54, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 54, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 33, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 33, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 33, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 33, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 33, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 33, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 33, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 33, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 33, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 21, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 21, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 21, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 22, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 22, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 22, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 22, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 10, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 21, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 21, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 21, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 21, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 11, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 17, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 10, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 8, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 5, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 4, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 3, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 1, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

The above shows the distribution of each operation, and there is no big step below:

[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 1, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

Of course, there is also a simple way to sort the list, and you only need to cycle the number of times the list length can be:

Data = [, 21, 6]
Numbers = []
# Locate the minimum value in the list
For I in range (len (data )):
Num = data. pop (data. index (min (data )))
Numbers. append (num)
Print (numbers)

The principle of the method is that we know that the purpose is to sort the elements in the list, so every time we find the minimum value in the list and use the pop method to pop it out, then the length of the list is reduced by 1 each time, and we only find the minimum value each time. We use index () to find the index of the value, and use another list to receive it. The running result is as follows:

[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

Related Article

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.