Python Bubble Sort

Source: Internet
Author: User

Bubble sort note three points:

    1. The first layer loops without looping through all the elements.
    2. The two-tier loop variable is associated with the first-level loop variable.
    3. The second loop, finally, must loop through all the elements in the collection.

Example code one:

    1. The first layer loops, looping only n-1 elements.
    2. When the first layer of the loop variable is n-1, the second layer loops all elements.
s = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]#  bubble_sort for in range (0, Len (s)-1) :    for in range (i + 1, 0,-1):        if s[j] < s[j-1]:              -1] = S[j-1], s[j] for in  range (0, Len (s)):    print(S[m])
View Code

Example code two:

    1. The first layer loops all elements.
    2. The second layer also loops through all the elements.
1s = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]2 3 4  forIinchRange (0, Len (s)):5      forJinchRange (I, 0, 1):6         ifS[J] < S[j-1]:7S[J], s[j-1] = s[j-1], S[j]8 9  forMinchRange (0, Len (s)):Ten     Print(S[m])
View Code

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