冒泡排序详解。
package com.suwei.forTest;
public class bubbleSort {
public static void main(String[] args) {
bubbleSort2();
}
private static void bubbleSort2() {
int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,
99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
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 i = 0; i < a.length; i++)//遍历数据并打印
System.out.println(a[i]);
}
}

浙公网安备 33010602011771号