冒泡排序算法的运作如下:
1 package test; 2 3 /** 4 * 冒泡排序时间复杂度O(n的平方) 5 * 6 * @author xixingwang 7 * 8 */ 9 public class BubbleSort { 10 public static void main(String[] args) { 11 int score[] = { 67, 69, 75, 87, 89, 90, 99, 100 }; 12 for (int i = 0; i < score.length - 1; i++) { // 最多做n-1趟排序 13 for (int j = 0; j < score.length - i - 1; j++) { // 对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的) 14 if (score[j] < score[j + 1]) { // 把小的值交换到后面 15 int temp = score[j]; 16 score[j] = score[j + 1]; 17 score[j + 1] = temp; 18 } 19 } 20 System.out.print("第" + (i + 1) + "次排序结果:"); 21 for (int a = 0; a < score.length; a++) { 22 System.out.print(score[a] + "\t"); 23 } 24 System.out.println(""); 25 } 26 System.out.print("最终排序结果:"); 27 for (int a = 0; a < score.length; a++) { 28 System.out.print(score[a] + "\t"); 29 } 30 } 31 }
原文地址:http://www.cnblogs.com/xxwacm/p/3833604.html