has been only know bubble sort, slightly low, today studied the Java fast sorting, the complexity of the algorithm, large o notation, divide and conquer the concept of the algorithm and so on have a certain understanding.
public Static voidmain (string[] Args) {int[] arr =New int[] {8, 8, 9, 20, 8, 85, 41, 7, 1, 76 }; Quiksort (arr,0, arr.length-1); } Private Static voidQuiksort (int[] arr,intLeftintright ) { //If the left is passed over right, it is returned; if(left >=right ) { return; } //use the leftmost number as the cardinality intBase =arr[left]; inti =left ; intj =right ; //i is not equal to J to keep looping, if I and J are in the same position, let the cardinality and I replace while(i! =J) {//If you scan from the right to a number less than the cardinality, stop while(arr[j] >= base && J >I) {j--; } //If you scan from left to a number larger than the cardinality, stop while(arr[i] <= Base && i <j) {i++; } if(i <j) {swap (arr, i, j); Printarr (arr); }} Swap (arr, left, i); Printarr (arr); //recursionQuiksort (arr, left, i-1); Quiksort (arr, i+ 1, right); } Private Static voidSwapint[] arr,inti1,intI2) { inttemp =arr[i1]; arr[i1]=arr[i2]; arr[i2]=temp; } Private Static voidPrintarr (int[] Arr) { for(intK = 0; K < arr.length; k++) {System.out.print (arr[k]+ ","); } System.out.println (); }
public static void main (string[] Args) {
Quiksortdemo q = new Quiksortdemo ();
int[] arr = new int[] {8, 8, 9, 20, 8, 85, 41, 7, 1, 76};
Q.quiksort (arr, 0, arr.length-1);
}
private void Quiksort (int[] arr, int left, int. Right) {
If the left is passed over right, it is returned;
If (left >= right) {
Return
}
Use the leftmost number as the cardinality
int base = arr[left];
int i = left;
Int J = right;
I is not equal to J to keep looping, if I and J are in the same position, let the cardinality and I replace
While (i! = j) {
If you scan from the right to a number less than the cardinality, stop
While (arr[j] >= base && J > I) {
j--;
}
If you scan from left to a number larger than the cardinality, stop
While (arr[i] <= base && i < J) {
i++;
}
If (i < j) {
Swap (arr, i, j);
Printarr (arr);
}
}
Swap (arr, left, i);
Printarr (arr);
Recursion
Quiksort (arr, left, i-1);
Quiksort (arr, i + 1, right);
}
private void swap (int[] arr, int i1, int I2) {
int temp = arr[i1];
arr[i1] = arr[i2];
arr[i2] = temp;
}
private void Printarr (int[] Arr) {
For (int k = 0; K < arr.length; K++) {
System.out.print (arr[k] + ",");
}
System.out.println ();
}
Java Quick Sort