Python (5)-bubble sort

Source: Internet
Author: User
Tags for in range

data = [Ten, 4, 3, 8, one, 5, ten, 2, 1, +, +] "' idea: How many elements are looped, and each cycle starts from the first element and compares it to the elements behind it, if Larger than the back, in exchange, the largest number of the first cycle will be placed at the end, the next cycle will be less traversing a single number.  ' for in Range ' (Len (data)-1):    the For in range (len (data ) (j):        if data[i] > data[i+1]:             data[i], data[i+1] = data[i+1], Data[i]print(data)

Modify the code so that it prints out the results after each loop

data = [Ten, 4, 3, 8, one, 5, six, 2, 1, +, +] "' idea: How many elements are looped, starting from the first element and comparing it to the elements behind it, if the "In  range "(len (data)-1):    for in range (len (data)j):        if data[i] > data[i+1]:             data[i], data[i+1] = data[i+1 ], Data[i]    print(data)

Print

C:\temp>Python3 test.py[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 54][4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 33, 54][4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 22, 33, 54][4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 21, 22, 33, 54][3, 4, 8, 5, 10, 2, 1, 11, 13, 17, 21, 22, 33, 54][3, 4, 5, 8, 2, 1, 10, 11, 13, 17, 21, 22, 33, 54][3, 4, 5, 2, 1, 8, 10, 11, 13, 17, 21, 22, 33, 54][3, 4, 2, 1, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][3, 2, 1, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][2, 1, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54][1, 2, 3, 4, 5, 8, 10, 11, 13, 17, 21, 22, 33, 54]

Python (5)-bubble sort

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.