Java Bubble sorting and selection sorting, java Bubble Sorting Selection
// Bubble sort
Package test;
Public class Maopaosort {
Public static void method (){
Int [] arr = new int [10];
For (int I = 0; I <arr. length; I ++ ){
Arr [I] = (int) (Math. random () * 100 + 1 );
}
System. out. println ();
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
For (int I = 0; I <arr. length-1; I ++ ){
For (int j = 0; j <arr. length-1-i; j ++ ){
Int tem;
If (arr [j] <arr [j + 1]) {
Tem = arr [j];
Arr [j] = arr [j + 1];
Arr [j + 1] = tem;
}
}
System. out. println ("this is the" + (I + 1) + "secondary sorting result :");
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
}
System. out. println ("the final sorting result is :");
For (int num: arr ){
System. out. print (num + "");
}
}
Public static void main (String [] args ){
Method ();
}
}
// Select sorting
Package SelectionSort;
Public class SelectionSort {
Public static void selectionsort (){
Int tem;
Int [] arr = new int [10];
For (int I = 0; I <arr. length; I ++ ){
Arr [I] = (int) (Math. random () * 100 + 1 );
}
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
System. out. println ("-------- gorgeous split line --------");
For (int I = 0; I <arr. length-1; I ++ ){
Int k = 0;
For (int j = 1; j <arr. length-I; j ++ ){
If (arr [k]> arr [j]) {
K = j;
}
}
System. out. println ("nth" + (I + 1) + "Times," + "the minimum number of" + (k + 1) + "in the array arr, is "+ arr [k]);
Tem = arr [arr. length-i-1];
Arr [arr. length-i-1] = arr [k];
Arr [k] = tem;
System. out. println ("no." + (I + 1) + "the sorting result is :");
For (int num: arr ){
System. out. print (num + "");
}
System. out. println ();
}
System. out. println ("the final result of sorting is ");
For (int num: arr ){
System. out. print (num + "");
}
}
Public static void main (String [] args ){
Selectionsort ();
}
}