Quick the Implementation
Quick Platoon, just like its name must be, the wind as fast. Is basically the quickest sort algorithm. The basic idea of a quick line is to select a segmented element. Sort this element out. All elements on the left side of this element are smaller than this element, and all elements to the right of this element are larger than this element. Then the left and right 2 arrays are sorted separately.
Suppose you have the following array (all I left objects are smaller than the Shard object.) All J right objects are larger than the Shard object. This sentence will be useful to know later.
First, we treat the element of index = 0 as a shard element.
Starting with the position of index = 1, find the first element greater than 49. Found 65 is greater than 49 so mark I = 2
Then starting from the end of the array to find the first less than 49, when found index = 6, value = 27 is less than 49 so mark.
Now swap the elements of I and J.
After the exchange, the element also conforms to the definition (all I to the left of the object is less than the Shard object because I will continue to look for the element to the right, all the elements I position now must be less than the Shard element, otherwise not conform to the definition I left the element is less than the Shard element) and (all J right object is greater than )
So you can go down.
Continue to find the element greater than 49 starting from the position i = 2, when i = 3 is found value = 97
Continue from the right to find J = 5 When 13 is less than 49
Swap location
Now I go ahead. Stop at index = 4 where 76 is greater than 49
J also continues to look to the left from the right. When j = 3 value = 13 less than 49
I >= J so do not swap position. At this point, we need to swap the elements of the Shard and the J position.
At this point, the position of index = 3 is already in the right position. 49 of the elements on the right are greater than 49, 49 to the left of the elements are less than 49
You can then sort the elements of 0-3 and the elements from 4 to 7.
Steal a picture to help understand
Concrete implementation of Https://github.com/Cheemion/algorithms/blob/master/src/com/algorithms/sort/QuickSort.java
There is no clear place, hope can give me a message. Because I feel like blogging is not very good. Rattle
Sort--quicksort Quick Row