N個數字來排序,
兩兩比較小靠前;
外層循環n-1,
內層循環n-1-i;
若要升序變降序,
就把“<”變“>”;
public static void bubbleSort(int []arr) {
for(int i =1;i<
arr.length
;i++) {
for(int
j
=
0
;j<arr.length-i;j++) {
if(arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}