1. Simple Steps (numbers increase progressively from left to right)
1. Identify the principal element, which can be any element. Here we always select the last most principal element.
2. Search from left to right, and place smaller than the principal component to the left.
3. After the end is found, the principal component is exchanged with the next number on the left that is less than the principal component.
In this way, the left side of a principal component is smaller than the principal component, and the right side is greater than the principal component.
II. Implementation Code
[Java]
- Public class QuickSort {
-
- Public static void main (String [] args ){
- Int [] arr = {6, 9, 2, 5, 8, 6 };
-
- QuickSort quickSortSample = new QuickSort ();
- QuickSortSample. quickSort (arr, 0, arr. length-1 );
-
- For (int j = 0; j <arr. length; j ++ ){
- System. out. print (+ arr [j]);
- }
- }
-
- Public void quickSort (int data [], int start, int end ){
- If (start <end ){
- Int partitionIndex = partition (data, start, end );
- QuickSort (data, start, partitionIndex-1 );
- QuickSort (data, partitionIndex + 1, end );
- }
- }
-
- Public int partition (int data [], int start, int end ){
- Int key = data [end]; // use the last element. data [hi] is the primary element.
- Int I = start-1;
- For (int j = start; j <end; j ++ ){
- If (data [j] <= key ){
- I = I + 1;
- Swap (data, I, j );
- }
- }
- Swap (data, I + 1, end );
-
- Return I + 1;
- }
-
- Private void swap (int data [], int I, int j ){
- Int temp = data [I];
- Data [I] = data [j];
- Data [j] = temp;
- }
-
- }
Iii. Step-by-step demonstration
6, 9, 2, 5, 8, 6
Current point |
Condition of exchange |
Exchanged Array |
I = 0, j = 0 |
6 <= 6 |
6 9 2 5 8 6 |
I = 1, j = 2 |
2 <= 6 |
6 2 9 5 8 6 |
I = 2, j = 3 |
5 <= 6 |
6 2 5 9 8 6 |
I = 3 |
I was six times smaller than I was. |
6 2 5 6 8 9 |
Current point |
Condition of exchange |
Exchanged Array |
I = 0, j = 1 |
2 <= 5 |
2 6 5 6 8 9 |
I = 1 |
I was less than five |
2 5 6 6 8 9 |
Current point |
Condition of exchange |
Exchanged Array |
I = 4, j = 4 |
8 <= 9 |
2 5 6 6 8 9 |
I = 5 |
I was always 9 small |
2 5 6 6 8 9 |
Iv. References
WIKI-sorting algorithm http://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7% AE %97%E6%B3%95
A series of Classical Vernacular algorithms-Quick Sort-quick http://blog.csdn.net/morewindows/article/details/6684558
Http://blog.csdn.net/v_JULY_v/article/details/6116297 of Quick Sort Algorithm
Deep analysis and http://blog.csdn.net/v_july_v/article/details/6211155 of the continuous and fast Sorting Algorithm
12 continued: http://blog.csdn.net/v_july_v/article/details/6262915 for all versions of the fast sort algorithm c/c ++