Java經典編程題50道之二十八
阿新 • • 發佈:2017-06-06
amp for 進行 temp 編程 排序 ati static bubble for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.print("\n排序後的數組為:");
for (int i : a) {
System.out.print(i+" ");
}
}
}
對10個數進行排序。
public class Example28 {
public static void main(String[] args) {
int[] s = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };
BubbleSort(s);
}
public static void BubbleSort(int[] a) {
System.out.print("原數組為:");
for (int i : a) {
System.out.print(i+" ");
}
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.print("\n排序後的數組為:");
System.out.print(i+" ");
}
}
}
Java經典編程題50道之二十八