Quick Sort is often adopted because its sorting efficiency is relatively high among several sorting methods that are all O(N*logN). In addition, the idea of quick sort—the divide-and-conquer method—is indeed practical. Therefore, many software companies like to test this in written tests and interviews, including well-known IT companies such as Tencent and Microsoft. Also, quick sort frequently appears in programming-related exams of various sizes, such as the Software Qualification Exam and the postgraduate entrance examination.
=a
Quick sort is a partition-exchange sort proposed by C.R.A.Hoare in 1962. It uses a divide-and-conquer strategy, usually called the divide-and-conquer method (Divide-and-Conquer Method).
The basic idea of this method is:
- 1. First take one number from the sequence as the pivot.
- 2. In the partition process, put all numbers greater than this number to its right, and all numbers less than or equal to it to its left.
- 3. Repeat step 2 for the left and right intervals until each interval has only one number.
- 1. i = L; j = R; dig out the pivot to form the first pit a[i].
- 2. j--, search from back to front for a number smaller than it. After finding it, dig out this number and fill it into the previous pit a[i].
- 3. i++, search from front to back for a number greater than it. After finding it, also dig out this number and fill it into the previous pit a[j].
- 4. Repeat steps 2 and 3 until i==j, then fill the pivot into a[i].
Although quick sort is called the divide-and-conquer method, these three words obviously cannot fully summarize all the steps of quick sort. Therefore, I have provided a further explanation of quick sort: pit digging and number filling + divide-and-conquer.
Let's look at an example first. The definition is given below (it's best to summarize the definition in your own words, as this will help with implementing the code).
Take an array as an example, and take the first number in the interval as the pivot.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
72 | 6 | 57 | 88 | 60 | 42 | 83 | 73 | 48 | 85 |
Initially, i = 0; j = 9; X = a[i] = 72
Since the number in a
Starting from j, search to the left for a number less than or equal to X. When j=8, the condition is met. Dig out a
The array becomes:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
48 | 6 | 57 | 88 | 60 | 42 | 83 | 73 | 88 | 85 |
i = 3; j = 7; X=72
Repeat the above steps: first search from back to front, then from front to back.
Search to the left starting from j. When j=5, the condition is met. Dig out a
Search to the right starting from i. When i=5, exit because i==j.
At this point, i = j = 5, and a
The array becomes:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
48 | 6 | 57 | 42 | 60 | 72 | 83 | 73 | 88 | 85 |
It can be seen that the numbers before a
Let's summarize pit digging and number filling:
Following this summary, it is easy to implement the code for pit digging and number filling:
Then write the code for the divide-and-conquer method:
Such code is obviously not concise enough, so combine and tidy it up:
There are many improved versions of quick sort, such as randomly selecting the pivot, or directly using another sorting method when there is less data in the interval to reduce the recursion depth. Interested friends can study it further.
Note 1: Some books use the middle number as the pivot. To implement this, it is very easy—just swap the middle number with the first number.
Author: MoreWindows
Original article: https://blog.csdn.net/morewindows/article/details/6684558