Python Bubble Sort Attention Essentials example detailed _python

Source: Internet
Author: User

Bubble sort note three points:

1. The first cycle does not have to cycle through all elements.

2. The two-layer cyclic variable is associated with the first-level loop variable.

3. Second-tier loops, which eventually must loop through all the elements in the collection.

Sample code One:

1. The first cycle, only n-1 elements.

2. When the first-level loop variable is n-1, the second layer loops through all the elements.

s = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]
# Bubble_sort for
i in range (0, Len (s)-1): For
J in range (i + 1, 0,-1): C7/>if S[j] < S[j-1]:
s[j], s[j-1] = s[j-1], s[j] for
m in range (0, Len (s)):
print (s[m))

Example code two:

1. The first layer loops all elements.

2. The second layer also circulates all elements.

s = [3 4, 1, 6, 2, 9, 7, 0, 8, 5] for
I in range (0, Len (s)):
for J in range (I, 0,-1):
if S[J] < s[j- 1]:
s[j], s[j-1] = s[j-1], s[j] for
m in range (0, Len (s)):
print (s[m))

The above is a small series to introduce the Python bubble sorting algorithm attention points, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.