Python bubble sorting considerations

Source: Internet
Author: User
This article introduces the python bubble sorting knowledge, which involves the main details of the bubble sorting. This article uses the example code to explain it to you, which is very detailed and has reference value, if you are interested, let's take a look. This article introduces you to the python bubble sorting knowledge, which involves the main details of bubble sorting. This article uses the example code to explain to you, which is very detailed, if you are interested in the reference, take a look.

">

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_sort
For 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.

The above is a detailed description of the main points of attention for Python bubble sorting. For more information, see other related articles in the first PHP community!

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.