Java Quick Sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.