Quick Sort (Java version)

(This point is the main sorting method is applied recursively) 

Package sorting;


Import java.util.Arrays;

/ **
* Quicksort
* /
public class the demo1 {
public static void main (String [] args) {
int A [] = { . 1,. 8, 2,. 4,. 3,. 9,. 5,. 6};
Go (A, 0, a.length -. 1);
System.out.println (of Arrays.toString (A));
}

/ **
* for sorting, find the cut-off value, and based on the boundary, the left and right parts sequencing continues
* /
public static void Go (int [] A, int left, int right) {
IF (left <right) {
int qsort MID = (A , left, right);
Go (a, 0, MID -. 1);
; Go (a, + MID. 1, right)
}
}

/ **
embodied sort --- * ---
* 1, is first left index value as a comparison element, sequentially from the right array moving forward, until the value is greater than the value of the right to the left, out of a while loop
* 2, perform switching method, the second loop
* 3, or to the left as a reference index, but the index cycle is advanced from the back left, to the right until the value greater than the value of the left side, second bounce while loop
* 4, perform switching method, a second round of cycle
* 5, when the left end of the whole cycle, after the value of the return left right> is the cut-off value
* /
public static int qsort (int [] a, int left, int right) {
the while (left <right) {
the while (left <right A && [left] <= A [right]) {
right -;
}
IF (left <right) {
the swap (A, left, right);
}
the while (left <right && A [left] <= A [right ]) {
left ++;
}
IF (left <right) {
swap(a, left, right);
}
}
return left;
}

/**
* 交换
*/
public static void swap(int[] a, int left, int right) {
int temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}



Guess you like

Origin www.cnblogs.com/Rando_M/p/11823483.html