這是我的代碼,使用選擇。我需要使用插入,並且不要使用臨時數組或ArrayList。我需要如何做插入排序的幫助。使用插入排序對象的arraylist排序
public static void sortStudents(ArrayList<Student> list)
{//selection sort
Student tempStudent;
int count1;
int count2;
int largest;
for (count1=0; count1<list.size()-1; count1++)
{
largest = 0;
for (count2=largest+1; count2<list.size()-count1; count2++)
{
if ((list.get(largest)).compareTo(list.get(count2)) < 0)
{
largest = count2;
}
}
tempStudent = list.get(list.size()-1-count1);
list.set(list.size()-1-count1, list.get(largest));
list.set(largest, tempStudent);
}
}
}
我喜歡在半夜做作業的氣味。 – whirlwin 2011-03-17 23:34:31
http://en.wikipedia.org/wiki/Insertion_sort有一個很好的解釋和僞代碼,它應該足夠綽綽有餘 – Voo 2011-03-17 23:35:25