Java quick sorting and java sorting

Source: Internet
Author: User

Java quick sorting and java sorting

// Fast sorting thought: select the last number (key) of the array, which is smaller than the former (key is placed in the middle, then, the recursive termination condition (the array has only one number). public class Sort <T extends Comparable <T >>{// can be compared, so it must inherit java. lang. comparablepublic void quick (T [] array) {sort (array, 0, array. length-1);} private void sort (T [] array, int left, int right) {if (left <right) {int p = partition (array, left, right); sort (array, left, P-1); sort (array, p + 1, right) ;}} private int partition (T [] array, int left, I Nt right) {int I = left-1; for (int j = left; j <right; j ++) {if (array [j]. compareTo (array [right]) <= 0) {I ++; // understand the details of int I = left-1 and return I + 1 !!! Swap (array, I, j) ;}} swap (array, I + 1, right); // Insert the key into return I + 1 ;} private void swap (T [] array, int I, int j) {T t = array [I]; array [I] = array [j]; array [j] = t ;}}


How can I implement a fast Sorting Algorithm Using JAVA?

Code specially compiled for you
Test

Public class QuickSort {

Public static int Partition (int a [], int p, int r ){
Int x = a [r-1];
Int I = p-1;
Int temp;
For (int j = p; j <= R-1; j ++ ){
If (a [J-1] <= x ){
// Swap (a [J-1], a [I-1]);
I ++;
Temp = a [J-1];
A [J-1] = a [I-1];
A [I-1] = temp;

}
}
// Swap (a [R-1, a [I + 1-1]);
Temp = a [r-1];
A [r-1] = a [I + 1-1];
A [I + 1-1] = temp;

Return I + 1;

}

Public static void QuickSort (int a [], int p, int r ){

If (p <r ){
Int q = Partition (a, p, r );
QuickSort (a, p, q-1 );
QuickSort (a, q + 1, r );

}

}

Public static void main (String [] stra ){

Int a [] = };
QuickSort (a, 1, 10 );

For (int I = 1; I <= 10; I ++)
System. out. println (a [I-1]);

}

}

Java quick sorting

I changed it for you:
Public class Point {
Public static void main (String [] args ){
Int [] a = {, 3 };
QuickSort (a, 0, a. length-1 );

For (int I = 0; I <a. length; I ++ ){
System. out. print (a [I] + ",");
}
}
Static int Partition (int [] a, int left, int right)
{
Int tmp;

// Perform a quick sorting and return the central record location
Int centers = a [left]; // place the center in a [0]
While (left <right)
{
While (left <right & a [right]> = cursor)
Right --;
// Move a smaller value than the central record to the lower end
Tmp = a [right];
A [right] = a [left];
A [left] = tmp;
While (left <right & a [left] <= left)
Left ++;
Tmp = a [right];
A [right] = a [left];
A [left] = tmp;
// Move the record larger than the central record to the high end
}
A [left] = centers; // The Center is moved to the correct position.
Return left; // return the center position.
}

Public static void quickSort (int [] a, int left, int right ){
If (left> = right-1)
Return;

Int Partition = Partition (a, left, right );
QuickSort (a, left, skip-1 );
QuickSort (a, iterator + 1, right );
}
}

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.