/** * 冒泡排序 * @author owen_liu * */ public class BubbleSort { public static void main(String[] args) { int a[] = {23, 38, 65, 33, 44, 13, 27, 49, 33, 34, 12, 64, 5, 4, 62}; sort(a); // for (int i = 0; i < a.length; i++) { // System.out.print(a[i] + ","); // } } public static void sort(int[] a) { int temp = 0; for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } for (int k = 0; k < a.length; k++) { System.out.print(a[k] + ","); } System.out.println(); } } }
運行結果:java
23,38,33,44,13,27,49,33,34,12,64,5,4,62,65,
23,33,38,13,27,44,33,34,12,49,5,4,62,64,65,
23,33,13,27,38,33,34,12,44,5,4,49,62,64,65,
23,13,27,33,33,34,12,38,5,4,44,49,62,64,65,
13,23,27,33,33,12,34,5,4,38,44,49,62,64,65,
13,23,27,33,12,33,5,4,34,38,44,49,62,64,65,
13,23,27,12,33,5,4,33,34,38,44,49,62,64,65,
13,23,12,27,5,4,33,33,34,38,44,49,62,64,65,
13,12,23,5,4,27,33,33,34,38,44,49,62,64,65,
12,13,5,4,23,27,33,33,34,38,44,49,62,64,65,
12,5,4,13,23,27,33,33,34,38,44,49,62,64,65,
5,4,12,13,23,27,33,33,34,38,44,49,62,64,65,
4,5,12,13,23,27,33,33,34,38,44,49,62,64,65,
4,5,12,13,23,27,33,33,34,38,44,49,62,64,65,code