Algorithm: Sorting
Watching “Aha! Algorithms”, I plan to make a reading note.
The first chapter is sorting, which describes three methods: bucket sort, bubble sort and quick sort.
bucket sort
I found it convenient at first, so I copied the C code in the original book with python I don’t know C language
The principle of this method is like when sorting a column of numbers, how big is the maximum (n)
in this column of numbers, set (n+1)
buckets (because 0
is to be included). Reading a number in the list (eg a
), then a flag is placed on the a
th bucket. Finally, traverse each bucket and display the bucket with the flag to complete the sorting.
``` python
flag = True
number_list = [] #input number list
book = [] #bucket
#Let the user enter a number, add the number to the list, and end the input if the user enters “done”
while flag:
number = input(“give me number to input:\n”)
if number == ‘done’:
flag = False
break
number_list.append(int(number))
flag = True
#Number of buckets, plus 1 because it contains 0
length = int(max(number_list)) + 1
#Set the number of flags in these buckets to 0
for i in range(length):
book.append(i)
book[i] = 0
#Insert the numbers in the number list as flags into the corresponding bucket
for i in number_list:
book[int(i)] += 1
Traverse the buckets from large to small, range(a,b,-1) a is the starting point of the traversal (inclusive), b is the end point (exclusive)
for i in range(len(book)-1, -1, -1):
#If the number of flags is greater than or equal to 1
if book[i] >= 1:
#Print several times if there are several flags
for j in range(0, int(book[i])):
print(i)
1 | If the input is (~~Replace the Enter key with a space to save space~~) |
Because bubble sort is a double nested loop, the time complexity is O(N2).
If our computer can run 1 billion times per second, then sorting 100 million numbers, bucket sort takes only 0.1 seconds, and bubble sort takes 10 million seconds, which is 115 days long!
quicksort
Suppose we sort the seven numbers 9, 4, 3, 7, 2, 6, 5
First set the leftmost 9 as the base number, then find numbers smaller than 9 from right to left, and find numbers larger than 9 from left to right:
9
, 4, 3, 7, 2, 6, 5
from right to left –> 5 is a number less than 9
9, 4, 3, 7, 2, 6, 5
From left to right –> 9 does not encounter a larger number than him, but encounters 5, when two numbers meet, then this number Swap with base number
5
, 4, 3, 7, 2
, 6, 9 Do this step again (5 is the base number) from right to left –> 2 is smaller than 5
5, 4, 3, 7
, 2
, 6, 9 from left to right –> 7 is bigger than 5
5, 4, 3, 2
, 7
, 6, 9 swap 2, 7
2
, 4, 3, 5
, 7, 6, 9 From right to left –> 7 coincides with 2, and exchanges positions with the base number 5
In this way, the left side of the benchmark number 5 is smaller than 5, and the right side is larger than 5. Continue to sort the left and right sides of 5 in this way:
2
, 4, 3, 5, 6, 7
, 9
2, 3
, 4, 5, 6, 7, 9 this is all sorted
The reason why quicksort is faster is because each exchange is a jump compared to bubble sort. When sorting, set a reference point, put all the books less than or equal to the reference point to the left of the reference point, and put all the books greater than or equal to the reference point to the right of the reference point. In this way, in each exchange, it will not only be exchanged between adjacent numbers like bubble sort, and the exchange distance will be much larger. Of course, in the worst case, two adjacent numbers may still be exchanged. Therefore, the worst time complexity of quicksort is the same as that of bubble sort, O(N2), and its average time complexity is O(NlogN).
1 | #Enter a list of numbers |