看板 java 關於我們 聯絡資訊
: ※ 引述《sysyu.bbs@ptt.cc (Stu student)》之銘言: : > 假設有宣告如下 : > String[] names = {“Bunny”, “Ape”, “Orange”, “Cabbage”, “Mickey”, “ : > Juice”}; : > 利用選擇性排序對 names 做排序 . : > 囧掉了QQ : > 有誰可以幫忙寫出相關QQ 我會自己在想想的QQ http://www.caterpillar.onlyfun.net/Gossip/ AlgorithmGossip/SelectionInsertionBubble.htm String有實作java.lang.Comparable介面,可以利用compareTo() 來直接知道誰大誰小… public class Test { public static void selectionSort(String[] strs) { for(int i = 0; i < strs.length - 1; i++) { int m = i; for(int j = i + 1; j < strs.length; j++) { if(strs[j].compareTo(strs[m]) < 0) { m = j; } } if(i != m) { swap(strs, i, m); } } } private static void swap(String[] strs, int i, int j) { String t; t = strs[i]; strs[i] = strs[j]; strs[j] = t; } public static void main(String[] args) { String[] names = {"Bunny", "Ape", "Orange", "Cabbage", "Mickey", "Juice"}; selectionSort(names); for(String name : names) { System.out.println(name); } } } -- 良葛格學習筆記 http://caterpillar.onlyfun.net/Gossip/ ※ 編輯: JustinHere 來自: 219.86.71.144 (11/10 22:13)