Java數據構造及算法實例:選擇排序 Selection Sort。本站提示廣大學習愛好者:(Java數據構造及算法實例:選擇排序 Selection Sort)文章只能為提供參考,不一定能成為您想要的結果。以下是Java數據構造及算法實例:選擇排序 Selection Sort正文
/**
* 選擇排序的思惟:
* 每次從待排序列中找到最小的元素,
* 然後將其放到待排的序列的最右邊,直到一切元素有序
*
* 選擇排序改良了冒泡排序,將交流次數從O(N^2)削減到O(N)
* 不外比擬次數照樣O(N)
*/
package al;
public class SelectSort {
public static void main(String[] args) {
SelectSort selectSort = new SelectSort();
int[] elements = { 14, 77, 21, 9, 10, 50, 43, 14 };
// sort the array
selectSort.sort(elements);
// print the sorted array
for (int i = 0; i < elements.length; i++) {
System.out.print(elements[i]);
System.out.print(" ");
}
}
/**
* @author
* @param array 待排數組
*/
public void sort(int[] array) {
// min to save the minimum element for each round
int min, tmp;
for(int i=0; i<array.length; i++) {
min = i;
// search for the minimum element
for(int j=i; j<array.length; j++) {
if(array[j] < array[min]) {
min = j;
}
}
// swap minimum element
tmp = array[i];
array[i] = array[min];
array[min] = tmp;
}
}
}
"daima">第一行創立的路由是:/users/rss
第二行創立的路由是:/users/1/profile
“1”就是user_id,我們須要曉得用戶ID能力獲得用戶的profile.
第三行創立的路由是:/users/new/draft