Java Bubble SortingCode
Bubble Sorting
Algorithm The operation is as follows:
- Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
- Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
- Repeat the preceding steps for all elements except the last one.
- Continue to repeat the above steps for fewer and fewer elements until no one pair of numbers needs to be compared.
package COM. li. test; public class testsort1 {public static void main (string [] ARGs) {int arr [] = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int temp = 0; // after the first external loop ends, the maximum value is reached at the end. After the second external loop ends, the maximum value is reached at the second to the last, similarly, for (INT I = 0; I <10; I ++) {for (Int J = 0; j <9-I; j ++) {// here is 9-iif (ARR [J]> arr [J + 1]) {temp = arr [J + 1]; arr [J + 1] = arr [J]; arr [J] = temp ;}}for (INT I = 0; I <10; I ++) {system. out. print (ARR [I]) ;}}