冒泡排序例题

 1 int []g=new int[] {49,38,65,97,76,13,27,49};
 2             int m=0;
 3             System.out.print("原始顺序");
 4             for(int t:g)
 5             {
 6                 System.out.print(t+" ");
 7             }System.out.println();
 8             for(int i=0;i<g.length-1;i++)
 9             {
10                 for(int j=0;j<g.length-1-i;j++)
11                 {
12                     if(g[j]>g[j+1])
13                     {
14                         int x=g[j];
15                         g[j]=g[j+1];
16                         g[j+1]=x;
17                     }
18                     m++;
19                 }
20                 System.out.print((i+1)+"次循环");    
21                 for(int t:g)
22                 {
23                     System.out.print(t+" ");
24                 }System.out.println();
25             }        
26             System.out.println("循环了"+m+"次");
View Code

posted on 2016-02-29 18:41  beens  阅读(1019)  评论(0)    收藏  举报

导航