public class Bubblesort {
public void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length - 1; i++) {// 外循环次数减1
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] array = { 5345, 34, 543, 5, 435, 435, 21421, 4, 6879, 8, 9, 4, 1, 5, 7, 8, 9, };
Bubblesort insert = new Bubblesort();
insert.bubbleSort(array);
for (int i : array) {
System.out.print(i + ",");
}
}
}
java实现冒泡排序
最新推荐文章于 2024-07-24 16:24:19 发布