Python implements the bubbling ordering of data structures and algorithms

Source: Internet
Author: User

Bubble sort

The basic idea of bubble sorting is to compare two adjacent elements each time and swap them out if they are in the wrong order.

If there are n numbers to sort, just n?1 the number of digits, which means
N-1 operation. The "Every trip" requires a comparison of two adjacent numbers starting from the 1th position, and the smaller one
In the back, when the comparison is complete, move back one to continue comparing the size of the two adjacent numbers below, repeat this step until the last
Number that has not been returned, the number that has been returned does not need to be compared.

Code
1 def_bubblesort ():2A = []3n = Int (input ('Do you need to enter a few numbers to sort? '))4K=15      forIinchrange (n):6A.append (int (Input ('Please enter the number of%i:'%k )))7K+=18 9      forIinchRange (n-1):Ten          forJinchRange (n-i-1): One             ifA[J] < a[j+1]: AA[J], a[j+1] = a[j+1], A[j] -     Print 'The bubble sort result is:' -          forIinchA: the             PrintI -  - if __name__=='__main__': -_bubblesort ()

Python 2.7.9

Python implements the bubbling ordering of data structures and algorithms

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.