排序算法-冒泡排序

 1 public class TestDemo {
 2     public static void main(String[] args) {
 3         
 4         //建一个长度为10的int数组,给这个数组赋值,并打印
 5         int[] a = new int[10];
 6         for(int i=0;i<a.length;i++) {
 7             a[i] = 10-i;
 8         }
 9         for(int i=0;i<a.length;i++) {
10             System.out.print(a[i]+" ");
11         }
12         System.out.println();
13         
14         //冒泡排序,并打印数组
15         for(int i=0;i<a.length;i++) {
16             for(int j=i+1;j<a.length;j++) {
17                 if(a[i]>a[j]) {
18                     int temp = a[i];
19                     a[i] = a[j];
20                     a[j] = temp;
21                 }
22             }
23         }
24         for(int i=0;i<a.length;i++) {
25             System.out.print(a[i]+" ");
26         }
27         
28     }
29 }
30 
31 
32 Console:
33 10 9 8 7 6 5 4 3 2 1 
34 1 2 3 4 5 6 7 8 9 10 
原文地址:https://www.cnblogs.com/xingyazhao/p/6789443.html