Several ways to implement bubble sort in python

Source: Internet
Author: User
Tags rounds

What is bubble sort? Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.   it repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The name of the algorithm is because the larger the element will slowly "float" to the top of the sequence of the series, so the name bubble sort.  The above is the official explanation of the bubble sort of Baidu entry.  but I would like to say my personal understanding, I think the core idea of bubble sort is: compare two numbers each time, if they are wrong in order (greater or less than), then replace them. For example, if you want to sort five unordered numbers in ascending order (that is, from small to large), how do you use bubble sorting?
  1. First, the size of the first and second numbers is compared, because it is arranged from small to large, so if the first number is greater than the second number, the two numbers are swapped for the position, whereas the other is unchanged.
  2. Then the second and third numbers are compared, ibid.
  3. By comparing the rounds in turn, you will find that a total of 4 times, that is, if there are n numbers to compare, then it takes n-1 times to complete.
  4. The above process mainly completed a round of comparisons, finally determined a maximum number, and ranked at the end of 5 numbers, that is, the fifth number.
  5. It also means that you need to make a comparison of the first number to the fourth number to determine the maximum value.
  6. Then from the first number to the third number ...
  7. So the law is very obvious, five numbers need to compare four rounds, you can put 5 numbers in ascending order, so n number need to compare n-1 wheel.
above is the bubble sort implementation idea, next look at the code!

How is it implemented?

How is this going to be achieved? Look at the above analysis, I believe you can also make up!

Let's see what I've made of Python:

Method One: General implementation bubble sort
# Method defines a list to hold the numeric list= [] whileTrue: # Custom input number print ('How many numbers do you want to arrange? ')    Try: Num=int(Input ()) forIinchrange (num): a=int(Input ('Please enter section'+ STR ((i+1)) +'an integer:') ) List.append (a) except Valueerror:print ('wrong input! '# Bubble Sort core code, forJinchRange (len list)-1):         forKinchRange (len list)-1):            ifList[k] < list[k+1]: t=List[k] List[k]= list[k+1] List[k+1] =t Print (list)

The advantages and disadvantages of the algorithm mainly look at its time complexity, the time complexity of the bubble sort is: O (n^2)
It can be seen that the bubble sort time complexity is high, so it is not the optimal algorithm!

method Two: Using sorted () method to realize the sorting quickly
# define a list object to save the number list=[]print ('How many numbers do you want to arrange? ')Try: Num=int(Input ()) forIinchrange (num): a=int(Input ('Please enter section'+ STR ((i +1)) +'an integer:') ) List.append (a) except Valueerror:print ('wrong input! '# Use the sorted () method to sort and use the reverse field to implement descending print (sorted (list, reverse=true))

 It is highly recommended to use the sorted () method for sorting, because it is simple! Python is in the name of simplicity, the less code to achieve the same functionality, why not!

Several ways to implement bubble sort in python

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.