Java Bubble sorting, java bubble
Bubble sorting principle. The following uses ascending order as an example.
- Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
- Perform the same operation on each adjacent element, starting from the first pair to the end of the last pair. At this time, the last element is the largest.
- Repeat the preceding steps for all elements except the last one.
- Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.
1 public class BubbleSort {2 public static void main (String [] args) {3 int score [] = {67, 69, 75, 87, 89, 90, 99,100 }; 4 for (int I = 0; I <score. length-1; I ++) {// do a maximum of N-1 sequential sorting 5 for (int j = 0; j <score. length-I-1; j ++) {// score the current unordered range [0 ...... length-i-1] sorting (j range is critical, this range is gradually reduced) 6 if (score [j] <score [j + 1]) {// swap a small value to the next 7 int temp = score [j]; 8 score [j] = score [j + 1]; 9 score [j + 1] = temp; 10} 11} 12 System. out. print ("no." + (I + 1) + "secondary sorting result:"); 13 for (int a = 0; a <score. length; a ++) {14 System. out. print (score [a] + "\ t"); 15} 16 System. out. println (""); 17} 18 System. out. print ("final sorting result:"); 19 for (int a = 0; a <score. length; a ++) {20 System. out. print (score [a] + "\ t"); 21} 22} 23}
Java Bubble Sorting
Public class MySort {
Public static void main (String [] args ){
MySort sort = new MySort ();
Int [] arr = new int [] {, 22, 5 };
Sort. sort (arr );
For (int I: arr ){
System. out. print (I + ",");
}
}
Public void sort (int [] targetArr) {// small to large sorting
Int temp = 0;
For (int I = 0; I <targetArr. length; I ++ ){
For (int j = I; j <targetArr. length; j ++ ){
If (targetArr [I]> targetArr [j]) {
/* // Method 1:
Temp = targetArr [I];
TargetArr [I] = targetArr [j];
TargetArr [j] = temp;
// Method 2:
TargetArr [I] = targetArr [I] + targetArr [j];
TargetArr [j] = targetArr [I]-targetArr [j];
TargetArr [I] = targetArr [I]-targetArr [j]; */
}
}
}
}
}
Java Bubble Sorting
Public class MySort {
Public static void main (String [] args ){
MySort sort = new MySort ();
Int [] arr = new int [] {, 22, 5 };
Sort. sort (arr );
For (int I: arr ){
System. out. print (I + ",");
}
}
Public void sort (int [] targetArr) {// small to large sorting
Int temp = 0;
For (int I = 0; I <targetArr. length; I ++ ){
For (int j = I; j <targetArr. length; j ++ ){
If (targetArr [I]> targetArr [j]) {
/* // Method 1:
Temp = targetArr [I];
TargetArr [I] = targetArr [j];
TargetArr [j] = temp;
// Method 2:
TargetArr [I] = targetArr [I] + targetArr [j];
TargetArr [j] = targetArr [I]-targetArr [j];
TargetArr [I] = targetArr [I]-targetArr [j]; */
}
}
}
}
}