Java 插入排序一个内生的卡片列表
我有一个方法,在一个内生的双链表中对一手牌进行排序Java 插入排序一个内生的卡片列表,java,compare,insertion-sort,Java,Compare,Insertion Sort,我有一个方法,在一个内生的双链表中对一手牌进行排序 public void sort(Comparator<Card> cmp) { // source code that I am adapting my code from // for (int i=0; i < a.length; i++) { // /* Insert a[i] into the sorted sublist */ //
public void sort(Comparator<Card> cmp) {
// source code that I am adapting my code from
// for (int i=0; i < a.length; i++) {
// /* Insert a[i] into the sorted sublist */
// Object v = a[i];
// int j;
// for (j = i - 1; j >= 0; j--) {
// if (((Comparable)a[j]).compareTo(v) <= 0) break;
// a[j + 1] = a[j];
// }
// a[j + 1] = v;
// }
for(Card f = first; f!=null; f = f.next){
Card insert = f;
for(Card l = last; l!=null || l!= f; l = l.prev)
{
Card compare = l;
if(cmp.compare(compare, insert) <=0)
remove(insert);
this.add(insert);
break;
}
}
// Make sure to test invariant before and after
// You may find it helpful to use "remove" (but watch about size!)
}
公共无效排序(比较器cmp){
//我从中改编代码的源代码
//for(int i=0;i=0;j--){
//如果(((可比的)a[j])。与(v)相比,快速看一眼,我看到三个bug。可能还有更多
- 我很确定您想要的不是
l!=null | | l!=f
,而是&&
——您所写的条件总是正确的
- 您应该使用
{}
来界定if
的“true”分支。当前,this.add(insert);
和break;
将运行if
条件是真还是假-缩进表明这不是您所期望的
- 在
this.add(insert)
中,您没有指定要在列表中的何处添加新节点。我希望看到一些内容指定您将在l
指示的位置添加新节点
通过使用调试器逐步完成此操作,您学到了什么?