Python Sort and search
Learn a bit of sorting and search, do a summary. If it's not right there, please give me more advice.
Sorting algorithm: is an algorithm that can arrange a string of data in a particular order.
stability : A stable sorting algorithm maintains a relative order of records that have equal key values. That is, if a sorting algorithm is stable, when there are two equal key values of the record r and S, and in the original list R appears before s, in the sorted list R will also be before S.
For example (1,3) (2,3) (2,1) to sort
Bubble Sort :
Compares the adjacent elements. If the first one is larger than the second (ascending), swap them both. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
#Coding=utf-8defShenxu (alist): forJinchRange (len (alist) -1,0,-1):#Range (start number, end number, step) Len length forIinchRange (j):ifAlist[i] > Alist[i+1]: Print "before Transformation", I,alist, Alist[i], alist[i+1] = alist[i+1], alist[i]#Two number Exchange Print "after Transformation", I,alistlist1= [1,9,8,2,3,7,4,6,0,5]shenxu (List1)
Results
Bubbling improved once called directional bubbling sort; It feels like bubbling and doesn't say much.
- Worst time complexity: O (n2)
- Stability: Stable
Select Sort: is a simple and intuitive sorting algorithm. It works as follows. First find the smallest (large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and place it at the end of the sorted sequence. And so on until all elements are sorted.
(Continue writing next time)
Python Sort and search