这是我的代码,使用选择。我需要使用插入,并且不要使用临时数组或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