java实现冒泡排序

冒泡排序:依次比较相邻的两个数,将小数放前面,大数放后面;

 1 public class 冒泡 {
 2 public static void main(String[] args) {
 3     //定义一个数组并且初始化
 4     int[]a = {12,15,42,31,26,63,23};
 5     //外循环,需要判断的轮数
 6     for (int j = 0; j < a.length - 1; j++) {
 7         //内循环,每一次都将大的值放到最后面
 8     for (int i = 0; i < a.length - 1; i++) {
 9         if (a[i] > a[i+1]) {
10             int temp = 0;
11             temp = a[i];
12             a[i] = a[i+1];
13             a[i+1] = temp;
14         }
15     }
16 }
17     //输出每一个数组型号
18     for(int k=0;k<a.length - 1;k++) {
19         System.out.print(a[k] + " ,");
20     }
21     
22 }
23 }
原文地址:https://www.cnblogs.com/yekaiit/p/14794832.html