1 package demo; 2 3 import java.util.Arrays; 4 5 public class Demo02 { 6 public static void main(String[] args) { 7 int[] arr = new int[10]; 8 for(int i=0; i<arr.length; i++){ 9 arr[i] = (int) (Math.random()*100); 10 } 11 System.out.println("冒泡排序前:"); 12 System.out.println("第"+(0)+"次:"+Arrays.toString(arr)); 13 14 bubbleSort(arr); 15 System.out.println("冒泡排序前:"); 16 System.out.println("第"+(0)+"次:"+Arrays.toString(arr)); 17 } 18 /* 19 * 冒泡排序 20 */ 21 public static void bubbleSort(int a[]) { 22 for (int i = 0; i < a.length - 1; i++) { 23 for (int j = 0; j < a.length - 1; j++) { 24 if (a[j] > a[j + 1]) { 25 int temp = a[j]; 26 a[j] = a[j + 1]; 27 a[j + 1] = temp; 28 } 29 } 30 System.out.println("第"+(i+1)+"次:"+Arrays.toString(a)); 31 } 32 } 33 }
时间: 2024-10-06 13:37:18