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實現冒泡排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.