Java Basic algorithm-Quick sort

Source: Internet
Author: User

Play Blog Park For many years, the first time to write something, start with the foundation. Recently went to the interview, found that their algorithm forgot, hurriedly review under.

/**
*
* Sorting algorithm Test class
*
*/
public class Testforsort {

/**
* @param args
*/
public static void Main (string[] args) {
int[] array = new int[] {4,1,5,3,2,6};
Quick Line
Quicksort (array,0,array.length-1);
System.out.println ("======= final =========");
for (int a:array) {
System.out.print (a);
}
}

/**
* Quick Sort
*
* @param int n[] array to sort
* @param left sort start subscript
* @param Right Sort end subscript
* @return
*/
static void quicksort (int n[], int left, int. right) {
int DP;
if (left < right) {
DP = partition (n, left, right);//Division method, return to the middle axis subscript
Quicksort (n, left, dp-1);//recursive Quick row
Quicksort (N, DP + 1, right);
}
}

/**
* Fast core algorithm, and return to the middle axis subscript
* @param n the array to divide
* @param left first subscript
* @param right bottom subscript
* @return
*/
static int partition (int n[], int left, int. right) {

The first of the array is the middle axis (the left value is reserved)
int pivot = N[left];
System.out.println ("================== ==================== before sorting");
System.out.println ("middle axis value =" +pivot);
System.out.println ("======= per division =========, left=" +left+ ", n[left]=" +n[left]+ ", right=" +right+ ", n[right]=" +right+ "" );
Left < Right to execute, that is, until i=j, when the location of the IJ collision is the middle axis
while (left < right) {
Find the first number smaller than the middle axis n[rigth]
while (left < right && N[right] >= pivot) {
right--;
}
Assign to N[left] The first n[right that is smaller than the middle Axis], and then left to the right to move a
if (left < right) {
System.out.println ("n[" +right+ ") is assigned to n[" +left+ "],left right Shift one");
n[left++] = N[right];
}
Find the first number larger than the middle axis n[left]
while (left < right && N[left] <= pivot) {
left++;
}
Assign the first number N[left] to n[right] and right to the left one
if (left < right) {
System.out.println ("n[" +left+ ") is assigned to n[" +right+ "],right right Shift one");
n[right--] = N[left];
}
}
The middle axis assigns the final left value (the next recursive middle axis subscript)
N[left] = pivot;
System.out.println ("================== ==================== after sorting");
System.out.println ("middle axis value =" +pivot+ "subscript =" +left ");
System.out.println ("left=" +left+ ", n[left]=" +n[left]+ ", right=" +right+ ", n[right]=" +right+ "");

for (int a:n) {
System.out.print (a);
}
System.out.println ();
Back to mid-axis subscript
return left;

}
}

Java Basic algorithm-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.