Public class SortAll {
/**
* Java Implementation of Bubble sorting, selection sorting, insertion sorting, and Shell sorting
*/
Public static void main (String [] args ){
Int [] I = {1, 5, 6, 12, 4, 9, 3, 23, 39,403,596, 87 };
System. out. println ("---- result of Bubble Sorting :");
MaoPao (I );
System. out. println ();
System. out. println ("---- select the sorting result :");
XuanZe (I );
System. out. println ();
System. out. println ("---- Insert the sorting result :");
ChaRu (I );
System. out. println ();
System. out. println ("---- result of Shell sorting :");
Shell (I );
}
// Bubble sort
Public static void maoPao (int [] x ){
For (int I = 0; I <x. length; I ++ ){
For (int j = I + 1; j <x. length; j ++ ){
If (x [I]> x [j]) {
Int temp = x [I];
X [I] = x [j];
X [j] = temp;
}
}
}
For (int I: x ){
System. out. print (I + "");
}
}
// Select sorting
Public static void xuanZe (int [] x ){
For (int I = 0; I <x. length; I ++ ){
Int lowerIndex = I;
// Find the smallest Index
For (int j = I + 1; j <x. length; j ++ ){
If (x [j] <x [lowerIndex]) {
LowerIndex = j;
}
}
// Exchange
Int temp = x [I];
X [I] = x [lowerIndex];
X [lowerIndex] = temp;
}
For (int I: x ){
System. out. print (I + "");
}
}
// Insert sorting
Public static void chaRu (int [] x ){
For (int I = 1; I <x. length; I ++) {// I from the beginning, because the first number is already sorted
For (int j = I; j> 0; j --){
If (x [j] <x [j-1]) {
Int temp = x [j];
X [j] = x [j-1];
X [j-1] = temp;
}
}
}
For (int I: x ){
System. out. print (I + "");
}
}
// Sort by hill
Public static void shell (int [] x ){
// Group
For (int increment = x. length/2; increment> 0; increment/= 2 ){
// Sort each group
For (int I = increment; I <x. length; I ++ ){
Int temp = x [I];
Int j = 0;
For (j = I; j> = increment; j-= increment ){
If (temp <x [j-increment]) {
X [j] = x [j-increment];
} Else {
Break;
}
}
X [j] = temp;
}
}
For (int I: x ){
System. out. print (I + "");
}
}
}