1, The stability of sorting algorithm
Stability: the stable sorting algorithm will keep the records with equal key values in relative order. That is, if a sorting algorithm is stable, when there are two records R and S with equal key values, and R appears before S in the original list, R will also be before S in the sorted list.
2, Bubble sorting
1. The operation of bubble sorting algorithm is as follows:
- Compare adjacent elements. If the first one is larger than the second one (in ascending order), exchange the two of them.
- Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. When this is done, the last element will be the maximum number.
- Repeat the above steps for all elements except the last one.
- Continue to repeat the above steps every time for fewer and fewer elements, until no one pair of numbers needs to be compared.
2. Implementation of bubble sorting
[method 1]
def bubble_sort(alist): """Bubble sort""" n = len(alist) for j in range(0, n-1): #How many times to go? for i #[0,1,2,3...n-2] for i in range(0, n-1-j): #From the beginning to the end, explain as follows if alist[i] > alist[i+1]: alist[i], alist[i+1] = alist[i+1], alist[i] if __name__ == '__main__': li = [54,26,93,17,77,31,44,55,20] print(li) bubble_sort(li) print(li) # i 0~n-2 range(0, n-1) j=0 # i 0~n-3 range(0, n-1-1) j=1 # i 0~n-4 range(0, n-1-2) j=2 # i 0~n-5 range(0, n-1-3) j=3 # i 0~n-6 range(0, n-1-4) j=4 # When j=n i range (0, n-1-j) //Output: [54, 26, 93, 17, 77, 31, 44, 55, 20] [17, 20, 26, 31, 44, 54, 55, 77, 93]
[method 2]
def bubble_sort(alist): """Bubble sorting method 2""" n = len(alist) for j in range(n-1, 0, -1): #[n-1,n-2,n-3...1] for i in range(0, j): #From head to tail if alist[i] > alist[i+1]: alist[i], alist[i+1] = alist[i+1], alist[i] if __name__ == '__main__': li = [54,26,93,17,77,31,44,55,20] print(li) bubble_sort(li) print(li) //Output: [54, 26, 93, 17, 77, 31, 44, 55, 20] [17, 20, 26, 31, 44, 54, 55, 77, 93]
3. Time complexity of bubble sorting
- Optimal time complexity: O(n) (it means that there is no exchangeable element in the traversal, and the sorting ends.)
- Worst time complexity: O(n2)
- Stability: stable
3, Select sort
1. Principle of sorting by selection: first find the smallest (large) element in the unsorted sequence, store it at the beginning of the sorted sequence, then continue to find the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence. And so on until all elements are sorted.
2. Implementation of selective sorting
def select_sort(alist): """Selective sorting""" n= len(alist) for j in range(n-1): #j:0~n-2;j=[0,1,2,3...n-2] min_index = j for i in range(j+1, n): #J and j+1 Comparison if alist[min_index] > alist[i]: min_index = i alist[j], alist[min_index] = alist[min_index], alist[j] if __name__ == '__main__': li = [54,26,93,17,77,31,44,55,20] print(li) select_sort(li) print(li) //Output: [54, 26, 93, 17, 77, 31, 44, 55, 20] [17, 20, 26, 31, 44, 54, 55, 77, 93]
3. Time complexity of selection sorting
- Optimal time complexity: O(n2)
- Worst time complexity: O(n2)
- Stability: instability (consider the case that the maximum is selected each time in ascending order)
4, Insert sort
1. Insertion Sort
Its working principle is to build an ordered sequence, for unsorted data, scan from the back to the front in the sorted sequence, find the corresponding position and insert it. In the implementation of insertion sorting, in the process of scanning from the back to the front, it is necessary to repeatedly move the sorted elements back step by step to provide insertion space for the latest elements.
2. Implementation of insertion sort
def insert_sort(alist): """Insert sort""" n= len(alist) for j in range(1,n): i = j while i > 0: if alist[i] < alist[i-1]: alist[i], alist[i-1] = alist[i-1], alist[i] i -= 1 #alist[i] is smaller than the previous number else: break if __name__ == '__main__': li = [54,26,93,17,77,31,44,55,20] print(li) insert_sort(li) print(li) //Output: [54, 26, 93, 17, 77, 31, 44, 55, 20] [17, 20, 26, 31, 44, 54, 55, 77, 93]
3. Time complexity of selection sorting
- Optimal time complexity: O(n) (in ascending order, the sequence is already in ascending order)
- Worst time complexity: O(n2)
- Stability: stable