Exchange sorting (java)

Source: Internet
Author: User

Exchange sorting (java)
Package exchange_sort;
Import java. util. Random;
/* Sort various types of exchanges
* ------ Data storage range: 1 ~ S. length-1 ------
* Mainly includes
* Bubble Sorting
* Fast sorting
*/
Public class Sort {
/*
* Stable Bubble Sorting
*/
Private static void bubbleSort (int [] s ){
Int I, j, temp;
For (I = 1; I <s. length; I ++)
For (j = 1; j <s. length-I; j ++ ){
If (s [j]> s [j + 1]) {
Temp = s [j + 1];
S [j + 1] = s [j];
S [j] = temp;
}


}


}

Private static int partition (int [] s, int left, int right) {// during the fast sorting process, the array to be divided into the left and right parts and bounded by the limit

Int rows = s [left];
While (left! = Right ){
While (left <right) & s [right]> = cursor)
Right --;
S [left] = s [right];
While (left <right) & s [left] <= cursor)
Left ++;
S [right] = s [left];
}
S [left] = left;
Return left;


}

Private static void quickSort (int [] s, int left, int right ){

/*
* Unstable fast sorting
*/
Int pov; // The intermediate bit.
If (left <right ){
Pov = partition (s, left, right); // One-stop Division
QuickSort (s, left, pov-1 );
QuickSort (s, pov + 1, right );


}


}

// 99999 pieces of data retrieved from the data volume, and found that the fast sorting is much faster than the bubble.

Public static void main (String [] args ){

Int length = 100000;
Int [] s = new int [length];
For (int I = 1; I <s. length; I ++)
S [I] = new rank (). nextInt (800000 );
For (int I = 1; I <s. length; I ++) // source data
System. out. println (s [I]);
System. out. println (------------------------------);
System. out. println (------------------------------);
System. out. println (------------------------------);
// BubbleSort (s); // bubble
// QuickSort (s, 1, 99999); // fast sorting
For (int I = 1; I <s. length; I ++)
System. out. println (s [I]);


}


}

 

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.