Python Bubble Sorting example details, python bubble

Source: Internet
Author: User

Python Bubble Sorting example details, python bubble

Three notes for Bubble Sorting:

1. You do not need to cycle all elements in the first loop.

2. Two-layer cyclic variables are associated with the first-layer cyclic variables.

3. The second layer of the loop must eventually loop all elements in the set.

Sample Code 1:

1. Loop at the first layer, which only loops n-1 elements.

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

S = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5] # bubble_sortfor I in range (0, len (s)-1 ): for j in range (I + 1, 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])

Sample Code 2:

1. The first layer loops all elements.

2. The second layer also loops through 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 the key points of attention for the python Bubble sorting algorithm introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.