Graphical Sorting Algorithm Quick Sort - Paired-End Detection

 This is the best article I've seen explaining quicksort, shared as follows:

        If our computer can run 1 billion times per second, then sorting 100 million numbers, bucket sorting takes only 0.1 seconds, and bubble sorting takes 10 million seconds, which is 115 days, right? scary. Is there a sorting algorithm that does not waste space and can be faster? That's "quick sort"! Just hearing the name makes you feel very high-end.

 

        Suppose we now sort the 10 numbers "6 1 2 7 9 3 4 5 10 8". First, find a random number in this sequence as the reference number (don't be frightened by this term, it is a number used for reference, and you will know what it is used for later). For convenience, let the first number 6 be the base number. Next, you need to put all the numbers larger than the reference number in the sequence to the right of 6, and the numbers smaller than the reference number to the left of 6, similar to the following arrangement.

       3  1  2 5  4  6  9 7  10  8

 

        In the initial state, the number 6 is in the first position of the sequence. Our goal is to move 6 to a position in the middle of the sequence, let's say that position is k. Now we need to find this k, and take the kth bit as the dividing point, the numbers on the left are all less than or equal to 6, and the numbers on the right are all greater than or equal to 6. Come to think of it, is there a way you can do this?

 

        Let me give you a hint. Please recall how bubble sort, by "swapping", puts each number back in place step by step. At this point you can also achieve the goal through the "exchange" method. How to exchange step by step? How can the exchange be convenient and time-saving? Don't look down, take out the pen and draw on the paper. When I first learned the bubble sort algorithm in high school, I felt that bubble sort was a waste of time, and I could only compare two adjacent numbers each time, which was obviously too unreasonable. So I thought of a way, and later I found out that this is "quick sort", please allow me to be a little narcissistic (^o^).

        The method is actually very simple: start "probing" from both ends of the initial sequence "6 1 2 7 9 3 4 5 10 8". First find a number less than 6 from right to left, then find a number greater than 6 from left to right, and then swap them. Two variables i and j can be used here, pointing to the leftmost and rightmost of the sequence, respectively. We give these two variables nice names "sentry i" and "sentry j". At the beginning, let the sentinel i point to the leftmost of the sequence (ie i=1), pointing to the number 6. Let sentinel j point to the far right of the sequence (i.e. j=10), pointing to the number 8.

 

       First sentinel j began to dispatch. Because the base number set here is the leftmost number, it is very important that sentinel j is dispatched first (please think about why). Sentinel j moves to the left step by step (i.e. j--) until it finds a number less than 6 and stops. Next, the sentinel i moves to the right step by step (i.e. i++) until it finds a number greater than 6 and stops. Finally, sentinel j stopped in front of the number 5, and sentinel i stopped in front of the number 7.

 

 

 

 

       Now swap the values ​​of the elements pointed to by sentinel i and sentinel j. The sequence after the swap is as follows.

        6  1  2  5  9 3  4  7  10  8

 

 

 

        At this point, the first exchange is over. Next, sentinel j continues to move to the left (a friendly reminder that sentinel j must start first each time). He found 4 (smaller than the benchmark number 6, which satisfies the requirement) and stopped. Sentinel i also continued to move to the right. He found 9 (larger than the reference number 6, which meets the requirements) and stopped. At this time, the exchange is performed again, and the sequence after the exchange is as follows.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940033&siteId=291194637