[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: