对冒泡排序的一种优化(减少外层循环次数)

 今天碰巧看到了冒泡培训的舞蹈视频,不过在看的过程中受到一点启发,才有了这次优化。网上有很多对冒泡排序的优化,这里提供一种不同的方案,已有的不再讨论。冒泡排序效率问题,估计很少使用,这里只作为学习讨论之用。

 

优化方案:我们都知道,对于长度为n的数组来说,冒泡排序外层循环需要n-1趟排序。然而如果当某趟排序的最后两个元素不需要调换位置时,我们可以减少一次外层循环。例如:设j+1为此趟排序的最后一个元素,如果当arr[j]<=arr[j+1]时,则在之后的排序中,不需要其他元素再与arr[j]进行比较,即减少一次外层循环。

 

  1 package test;
  5 public class BubbleSort {
  9    public static void main(String[] args) {
 10 
 11       int[] arr = {5,0,1,6,2,7,3,8,4,9};
 1
 15       System.out.println("初始顺序为:");
 16 
 17       for(int i:arr){
 18 
 19          System.out.print(i + " ");
 20 
 21       }
 25       System.out.println();
 26 
 27       int[] result = bubbleSort(arr);

 31       System.out.println("排序后结果为:");
 32 
 33       for(int i:result){
 34 
 35          System.out.print(i + " ");
 36 
 37       }
 38 
 39      
 40 
 41    }
 42 
 43   
 44 
 45    public static int[] bubbleSort(int[] arr){
 46 
 47       int[] src = arr.clone();
 48 
 49       int times = 0;
 50 
 51       for(int i = 0; i < src.length -1; i++){
 52 
 53          System.out.println("-------------------------------");
 54 
 55          System.out.println("第" + (++times) + "次排序");
 56 
 57          for(int j = 0; j < src.length - 1 - i;j++){
 58 
 59             System.out.println("compare src["+j+"]="+src[j]+"andsrc["+(j+1)+ "]=" + src[j+1]);
 60 
 61             if(src[j] > src[j + 1]){
 62 
 63                 int tmp = src[j];
 64 
 65                 src[j] = src[j + 1];
 66 
 67                 src[j + 1] = tmp;
 68 
 69             }
 70 
 71             /**
 72 
 73              * 优化:
 74 
 75              * 如果此趟排序中最后两个元素不需要交换位置,则减少一趟排序
 76 
 77              */
 78 
 79             else if(j == src.length - 1 - i - 1){
 80 
 81                 i++;
 82 
 83             }
 84 
 85          }
 86 
 87         
 88 
 89          // 输出此趟排序结果
 90 
 91          for(int num:src){
 92 
 93             System.out.print(num + " ");
 94 
 95          }
 96 
 97          System.out.println();
 98 
 99       }
100 
101      
102 
103       System.out.println("共经历["+times+"]次排序");
104 
105       return src;
106 
107    }
108 
109   
110 
111 }

 

输入结果:

初始顺序为:

5 0 1 6 2 7 3 8 4 9

-------------------------------

第0次排序

comepare src[0]=5 and src[1]=0

comepare src[1]=5 and src[2]=1

comepare src[2]=5 and src[3]=6

comepare src[3]=6 and src[4]=2

comepare src[4]=6 and src[5]=7

comepare src[5]=7 and src[6]=3

comepare src[6]=7 and src[7]=8

comepare src[7]=8 and src[8]=4

comepare src[8]=8 and src[9]=9

0 1 5 2 6 3 7 4 8 9

-------------------------------

第2次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=5

comepare src[2]=5 and src[3]=2

comepare src[3]=5 and src[4]=6

comepare src[4]=6 and src[5]=3

comepare src[5]=6 and src[6]=7

comepare src[6]=7 and src[7]=4

0 1 2 5 3 6 4 7 8 9

-------------------------------

第3次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=5

comepare src[3]=5 and src[4]=3

comepare src[4]=5 and src[5]=6

comepare src[5]=6 and src[6]=4

0 1 2 3 5 4 6 7 8 9

-------------------------------

第4次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

comepare src[3]=3 and src[4]=5

comepare src[4]=5 and src[5]=4

0 1 2 3 4 5 6 7 8 9

-------------------------------

第5次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

comepare src[3]=3 and src[4]=4

0 1 2 3 4 5 6 7 8 9

-------------------------------

第7次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

0 1 2 3 4 5 6 7 8 9

共经历[6]次排序

排序后结果为:

0 1 2 3 4 5 6 7 8 9

 

 

为了对比,下面是优化之前的输出:

初始顺序为:

5 0 1 6 2 7 3 8 4 9

---------------------------------------------------

第0次排序

comepare src[0]=5 and src[1]=0

comepare src[1]=5 and src[2]=1

comepare src[2]=5 and src[3]=6

comepare src[3]=6 and src[4]=2

comepare src[4]=6 and src[5]=7

comepare src[5]=7 and src[6]=3

comepare src[6]=7 and src[7]=8

comepare src[7]=8 and src[8]=4

comepare src[8]=8 and src[9]=9

0 1 5 2 6 3 7 4 8 9

---------------------------------------------------

第1次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=5

comepare src[2]=5 and src[3]=2

comepare src[3]=5 and src[4]=6

comepare src[4]=6 and src[5]=3

comepare src[5]=6 and src[6]=7

comepare src[6]=7 and src[7]=4

comepare src[7]=7 and src[8]=8

0 1 2 5 3 6 4 7 8 9

---------------------------------------------------

第2次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=5

comepare src[3]=5 and src[4]=3

comepare src[4]=5 and src[5]=6

comepare src[5]=6 and src[6]=4

comepare src[6]=6 and src[7]=7

0 1 2 3 5 4 6 7 8 9

---------------------------------------------------

第3次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

comepare src[3]=3 and src[4]=5

comepare src[4]=5 and src[5]=4

comepare src[5]=5 and src[6]=6

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------

第4次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

comepare src[3]=3 and src[4]=4

comepare src[4]=4 and src[5]=5

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------

第5次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

comepare src[3]=3 and src[4]=4

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------

第6次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

comepare src[2]=2 and src[3]=3

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------

第7次排序

comepare src[0]=0 and src[1]=1

comepare src[1]=1 and src[2]=2

0 1 2 3 4 5 6 7 8 9

---------------------------------------------------

第8次排序

comepare src[0]=0 and src[1]=1

0 1 2 3 4 5 6 7 8 9

共经历[9]次排序

排序后结果为:

0 1 2 3 4 5 6 7 8 9

posted @ 2013-11-22 00:40  huaerfan  Views(1650)  Comments(0)    收藏  举报