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]);
}
}