In the process of daily research and development, we are always considering whether the programs we develop are efficient. A good program execution is inseparable from a deep understanding and mastery of algorithms. In the days to come, I will take everyone to review several common algorithms.
Let's learn about the quick sort algorithm together !
quick sort algorithm
Wikipedia introduction: Quick sort uses the divide and conquer strategy to divide a sequence (list) into two smaller and larger subsequences, then recursively sort the two subsequences, and finally merge to get a list from small to large the sequence of.
Smart friends will ask: What is the divide and conquer strategy?
Divide and conquer
The literal explanation is "divide and conquer", which is to divide a complex problem into two or more identical or similar sub-problems, and then divide the sub-problems into smaller sub-problems... until the last sub-problem can be solved simply and directly , the solution of the original problem is the combination of the solutions of the subproblems.
From this we can lead to the implementation steps of the quick sort algorithm we are talking about today :
- Obtain a value randomly from the data, and divide it into two data sets on the left and right according to the size of this value. The value of the data set on the left is smaller than this value, and vice versa
- Recursively call the data on both sides Step 1
- Combine all data
Quick sort algorithm implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
So far, we have revisited the quick sort algorithm !
Reposted from: Micro reading https://www.weidianyuedu.com