Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用链接整数节点的插入排序_Java_Linked List_Insertion Sort - Fatal编程技术网

Java 使用链接整数节点的插入排序

Java 使用链接整数节点的插入排序,java,linked-list,insertion-sort,Java,Linked List,Insertion Sort,嘿,我一直在尝试为我正在使用的类使用插入排序方法,我们被告知使用插入排序对整数链表进行排序,而不使用Java库中已有的链表类 这是我的内部节点类,我只将其单独链接,因为我还没有完全掌握循环双链接列表的概念 public class IntNode { public int value; public IntNode next; } 这是我在IntList类中的插入排序方法 public IntList Insertion() { IntNode current = head; whi

嘿,我一直在尝试为我正在使用的类使用插入排序方法,我们被告知使用插入排序对整数链表进行排序,而不使用Java库中已有的链表类

这是我的内部节点类,我只将其单独链接,因为我还没有完全掌握循环双链接列表的概念

public class IntNode
{
  public int value;
  public IntNode next;
}
这是我在IntList类中的插入排序方法

public IntList Insertion()
{
IntNode current = head;

while(current != null)
    {
    for(IntNode next = current; next.next != null; next = next.next)
        {
        if(next.value <= next.next.value)
            {
            int temp = next.value;
            next.value = next.next.value;
                next.next.value = temp;
            }           
        }
    current = current.next;
    }
return this;
}
public IntList插入()
{
节点电流=头;
while(当前!=null)
{
for(IntNode next=current;next.next!=null;next=next.next)
{

如果(next.value插入操作仅在插入的列表已排序的情况下有效-否则您只是随机交换元素。要开始,请从原始列表中删除一个元素并从中构造一个新列表-此列表只有一个元素,因此已排序。现在继续从列表中删除其余元素原始列表,在运行时将其插入新列表。最后,原始列表将为空,新列表将被排序。

每次都需要从列表中的第一个节点开始,循环应以列表的尾部-1结束 像这样

 public static IntList Insertion()
{
     IntNode current = head;
     IntNode tail = null;
     while(current != null&& tail != head )
     {
       IntNode next = current;
      for( ; next.next != tail;  next = next.next)
    {
    if(next.value <= next.next.value)
        {
        int temp = next.value;
        next.value = next.next.value;
            next.next.value = temp;
        }
    }
    tail = next;
   current = head;
  }
 return this;
publicstaticintlist插入()
{
节点电流=头;
IntNode tail=null;
while(当前!=null&&tail!=head)
{
IntNode next=当前;
for(;next.next!=tail;next=next.next)
{

如果(next.value我也同意Zim Zam的意见。 插入排序的循环不变量也指定了这一点:“按排序顺序排列的子数组”。 下面是我为插入排序实现的代码,我在其中创建了另一个链表,其中包含按排序顺序排列的元素:

Node newList=new Node();
        Node p = newList;
        Node temp=newList;
        newList.data=head.data;
        head=head.node;
        while(head!=null)
        {
            if(head.data<newList.data)
            {
                Node newTemp = new Node();
                newTemp.data=head.data;
                newTemp.node=newList;
                newList=newTemp;
                p=newList;
            }   
            else
            {
                while(newList!=null && head.data>newList.data)
                {
                    temp=newList;
                    newList=newList.node;
                }
                Node newTemp = new Node();
                newTemp.data=head.data;
                temp.node=newTemp;
                newTemp.node=newList;
                newList=p;
            }

            head=head.node;
        }
Node newList=new Node();
节点p=新列表;
节点温度=新列表;
newList.data=head.data;
head=head.node;
while(head!=null)
{
if(head.datanewList.data)
{
temp=新列表;
newList=newList.node;
}
Node newTemp=新节点();
newTemp.data=head.data;
温度节点=newTemp;
newTemp.node=newList;
newList=p;
}
head=head.node;
}

向我们展示一些结果样本这看起来不太像。另外,你的
if
测试似乎有点倒退;你是否尝试按降序排序?他们告诉我们,降序或升序排序并不重要,我认为这是奇数。在列表生成中,我将主方法传递一个整数,例如5,它将生成5个数字0和5。列表的传递与中的相同。我需要在代码中更改什么才能实现这一点?除了创建我的列表类的新实例外,您的列表类还需要能够在任意位置添加节点。您的while循环应该运行,直到原始列表为空;从原始列表中删除一个节点,然后使用for loop将其插入到新列表中。您不应该交换任何值,只应该删除和插入节点。谢谢aymankoo和所有帮助您的人,他们真是太棒了!