java選擇排序的實現
阿新 • • 發佈:2019-01-25
public class selectSort {
public void Sort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {// 比較兩個數的大小
int temp = array[i]; // 使用臨時變數temp儲存array[i]
array[i] = array[j];// 將j的值賦值給i
array[j] = temp;// 將臨時變數temp的值賦值給j
}
}
}
}
public static void main(String[] args) {
int[] array = { 543, 54, 5, 435, 34, 54, 35, 4352, 4, 54, 6768, 78, 32, 41, 4, 6, 8, 2, 5, 124 };
selectSort s = new selectSort();
s.Sort(array);
for (int i : array) {
System.out.print(i + ",");
}
}
}