由小到大冒泡排序
思路:将相邻两个数进行比较,将小的掉到前头,大的沉下去。
- public class MaoPao {
-
- public static void main(String[] args) {
- int temp;
- Scanner scanner = new Scanner(System.in);
- int[] a = new int[10];
- for (int i = 0; i < 10; i++) {
- a[i] = scanner.nextInt();
- }
- for (int j = 0; j < 9; j++) {
- System.out.println("第"+j+"次循环");
- for (int i = 0; i < 9 - j; i++) {
- System.out.println("第"+i+"趟比较");
- if (a[i] > a[i + 1]) {
- temp = a[i];
- a[i] = a[i + 1];
- a[i + 1] = temp;
- }
- }
- }
- for (int s = 0; s < a.length; s++) {
- System.out.println("a[" + s + "] : " + a[s]);
- }
- }
- }
public class MaoPao {
/**
* @param args
*/
public static void main(String[] args) {
int temp;
// 创建一个扫描对象!
Scanner scanner = new Scanner(System.in);
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = scanner.nextInt();
}
// 冒泡排序由小到大
for (int j = 0; j < 9; j++) {//进行9次循环,实现9趟比较
System.out.println("第"+j+"次循环");
for (int i = 0; i < 9 - j; i++) {//在每一趟中进行9-i次比较
System.out.println("第"+i+"趟比较");
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int s = 0; s < a.length; s++) {
System.out.println("a[" + s + "] : " + a[s]);
}
}
}
阅读(347) | 评论(0) | 转发(0) |