冒泡排序算法
/**
* 冒泡排序
*/
public static int[] bubbleSort(int[] array) {
int temp = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
运行
/**
* 待排序的数组
*/
private static int[] array = {
12, 10, 5, 9, 5, 32, 16, 1, 9, 99, 80, 3, 18, 19, 20, 25, 7, 15
};
public static void main(String[] args) {
int[] result = SortUtils.bubbleSort(array.clone());
System.out.println(Arrays.toString(result));
}
输出
[1, 3, 5, 5, 7, 9, 9, 10, 12, 15, 16, 18, 19, 20, 25, 32, 80, 99]