1、简单的冒泡排序
1 //冒泡算法一共两种 2 // -----冒泡算法(1) 3 int a[]={23,69,4,58,1,20}; 4 for (int i = 0; i < a.length-1; i++) { 5 for (int j = i+1; j < a.length; j++) { 6 if(a[i]>a[j]){ 7 a[i]=a[i]+a[j]; 8 a[j]=a[i]-a[j]; 9 a[i]=a[i]-a[j]; 10 } 11 } 12 } 13 for (int i = 0; i < a.length; i++) { 14 System.out.println(a[i]); 15 } 16 // -----冒泡算法(2) 17 int b[]={23,69,4,58,1,20}; 18 for (int i = 0; i < b.length-1; i++) { 19 for (int j = b.length-1; j>i; j--) { 20 if(a[j-1]>a[j]){ 21 a[j]=a[j]+a[j-1]; 22 a[j-1]=a[j]-a[j-1]; 23 a[j]=a[j]-a[j-1]; 24 } 25 } 26 } 27 System.out.println("-----------2----------"); 28 for (int i = 0; i < b.length; i++) { 29 System.out.println(a[i]); 30 } 31
2、对冒泡算法的小小优化
原文:http://www.cnblogs.com/bequt/p/5460007.html