[Python study notes, python Study Notes

Source: Internet
Author: User

[Python study notes, python Study Notes

We recommend a Visual website [Visual Algo]: URL = 'https: // visualgo.net/en/sorting'

This website provides the principles and processes of various sorting algorithms, which are displayed in a dynamic manner. In addition, the relevant pseudo-code and the specific step of code execution are also provided.

 

[Bubble Sorting]

The sequence to be sorted needs to be accessed repeatedly. During the visit, the size of two adjacent items is compared. If the order is incorrect, two items are exchanged. Therefore, the maximum value of the part to be sorted will be moved to the appropriate position for each pass.

This process looks like every item is bubbling to the final position, so it becomes a bubble sort.

The process of completing a visit is provided here, and the maximum value of this list is 93 to the rightmost end of the list.

 

[Implementation of a bubble sort]

You can view the specific execution process through print n and k.

[Performance analysis]

In the above implementation, regardless of the input list, the time complexity is O (n2), because the number of times to be compared is (n-1) + (n-2) +... + 1; the space complexity is O (1 ).

However, the best-case performance can be reduced to O (n) by recording the locations that do not need to be exchanged. The method is as follows:

 

Of course, we can use the python built-in function [sort ()] to sort data. For example:

 

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.