氣泡排序一組隨機數
阿新 • • 發佈:2019-02-15
public class Bubble { public void orderBynothing(){ int arr1[] = new int[200]; for (int x = 0; x<arr1.length;x++) { arr1[x] = new Random().nextInt(147); } for(int i = 0; i<arr1.length;i++){ for(int j = 0;j<arr1.length;j++){ if(arr1[i]>arr1[j]){ int re = arr1[i]; arr1[i] = arr1[j]; arr1[j] = re; } } } for (int z:arr1) { System.out.println(z); } } public static void main(String []args){ Bubble b = new Bubble(); b.orderBynothing(); } }
第一個for迴圈是給arr1陣列賦值,第二個是氣泡排序,第三個是輸出