public class Test {
static int[] a = { 3, 9, 4, 5, 2, 7, 1, 8 };
public static void main(String[] args) {
// printA(a);
printB(a);
}
// 选择排序
static void printA(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
int temp = 0;
if (a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
System.out.print(a[i]);
}
}
// 冒泡排序
static void printB(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j<a.length -i-1; j++) {
if (a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int x = 0; x < a.length; x++) {
System.out.print(a[x]);
}
}
}
选择排序-冒泡排序
最新推荐文章于 2023-10-03 11:53:44 发布