1 Packagelog;2 3 Public classTest4 {4 5 /**6 * Java algorithm---bubble sort7 * 8 * @paramargs9 */Ten Public Static voidMain (string[] args) { One //arrays that need to be sorted A intArr[] = {49, 20, 36, 51, 18, 94, 61, 31, 50 }; - //loop out the contents of the array -System.out.println ("Before sorting:"); the for(intA:arr) { -System.out.print (A + "\ T")); - } - System.out.println (); + - inttemp = 0; + for(inti = 0; i < arr.length-1; i++) { A for(intj = 0; J < arr.length-1-I; J + +) { at if(Arr[j] > arr[j + 1]) { -temp =Arr[j]; -ARR[J] = arr[j + 1]; -Arr[j + 1] =temp; - } - } in } - //loop out the contents of the array toSystem.out.println ("After sorting:"); + for(intA:arr) { -System.out.print (A + "\ T")); the } * System.out.println (); $ Panax Notoginseng } - the}
The following is the output of the console
Java algorithm----Sort----(3) Bubble sort