排序

 

 

 

 

 

 

 1 package com.unit3.test;
 2 
 3 public class Test8 {
 4 
 5     public static void main(String[] args) {
 6         int[] a= {14,2,454,5,1};
 7         
 8         // 冒泡排序
 9 //        for(int i=0;i<a.length-1;i++) {
10 //            System.out.println("第"+(i+1)+"轮循环比较");
11 //            for(int j=0;j<a.length-1-i;j++) {
12 //                System.out.println("第"+(i+1)+"轮,第"+(j+1)+"次比较");
13 //                if(a[j]>=a[j+1]) {
14 //                    int h=a[j];
15 //                    a[j]=a[j+1];
16 //                    a[j+1]=h;
17 //                }
18 //            }
19 //        }
20 //        
21         //选择排序
22 //        for(int i=0;i<a.length-1;i++) {
23 //            for(int j=i+1;j<a.length;j++) {
24 //                if(a[i]>=a[j]) {
25 //                    int h=a[i];
26 //                    a[i]=a[j];
27 //                    a[j]=h;
28 //                }
29 //            }
30 //        }
31         
32         //插入排序
33         for(int i=0;i<a.length-1;i++) {
34             for(int j=i+1;j>0;j--) {
35                 if(a[j]<=a[j-1]) {
36                     int h=a[j];
37                     a[j]=a[j-1];
38                     a[j-1]=h;
39                 }else {
40                     break;
41                 }
42             }
43         }
44         
45         for(int x:a) {
46             System.out.println(x);
47         }
48 
49     }
50 
51 }

 

posted on 2020-06-08 22:58  cherry_ning  阅读(52)  评论(0)    收藏  举报

导航