Python sort 2 bubble sort, python sort bubble

Source: Internet
Author: User

Python sort 2 bubble sort, python sort bubble
Python sort 2 bubble sort

If you understand the previous insertion sorting method, the Bubble sorting method is easy to understand. The Bubble sorting method is two methods that compare the sizes in the backward displacement mode. The SWAp process is not much better. The Code is as follows:

The first is an unordered list lis. The old rule is printed and compared with the final sorting. The function method bubbie_sort is Bubble sorting, and the count is also the list length len (list ), the first for loop gets the first value in the list and then shifts backward. The second for loop gets the list I + 1, that is, the second value is shifted backward, this table is the second for and will only compare the values after the first for. The count length is always 15. if the two are different, an error out of the range will be reported. if lis [0] is determined: 10> li [1]: 30 if 10 is greater than 30, then execute the subsequent operation. Here, the abbreviation is a, B = B, and a is the same as the two values are exchanged. If it is not greater than, it will not change. j. The backward displacement will continue to be consistent. After the comparison of the last lis value, the cycle ends. as long as the condition is true, the comparison can be printed in the loop if you do not understand it, the Code is as follows:

 

In this way, we can intuitively display the internal sorting method. In the first loop, we will keep the smallest value at the beginning, which is equivalent to repeating the entire list and finding the smallest value at the beginning, the second cycle is to put the smallest one into the second place, and so on. It is like there are a lot of books to put on the shelf. If you want to move from left to right, from small books to big books, at a glance, you can see the size of the book on the shelf in sequence. Of course, the program is not as convenient as we can intuitively judge, so it compares all the books with a clear understanding, then you can operate based on the rules you specify. Its advantages are computing power.

Well, after reading it for half a day, it's time to digest it. You can do some similar things to consolidate and improve it. You are welcome to watch it. See you next time.

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.