如何使用插入排序对对象数组进行排序?

时间:2014-01-27 18:26:36

标签: java arrays algorithm sorting

有人可以解释为什么我的插入排序不起作用吗?您不必为我编写代码。带领我走向我需要去的方向。

import java.util.Random;

public class Storage 
{
private Node[] id;
private int counter;

public Storage(int size)
{
    id = new Node [size];
    counter = 0;
}

public void addTo(int number, long time)
{
    for (int i = 0; i < id.length; i++)
    {
        id[i] = new Node(number, time);
    }
}

public String toString()
{
    String output = id [counter] + "\t";
            return output;
}

public int myRand()
{
    int r;
    Random gen = new Random();
    return r = gen.nextInt(201);
}

public long tellTime()
{
    long clock;
    return clock = System.nanoTime();
}

public void sortNode()  //InerstionSort
{
    int j;
    Node temp;
    for (int i = 1; i < id.length; i++)
    {
        j = i;
        temp = id[i];

        while (j != 0 && id[j-1].getNumber() > temp.getNumber())
            {
                id[j] = id[j-1];
                j--;
            }

        id[j] = temp;
    }
    }
}
}



public class Node 
{
private int number;
private long time;

public Node(int n, long t)
{
    number = n;
    time = t;
}

public String toString()
{
    String output = number + "\t\t" + time + "\n";

    return output;
}


public int getNumber()
{
    return number;
}

public long getTime()
{
    return time;
}

}


public class Driver 
{

static Storage storage = new Storage(50);
static Storage store = new Storage(50);
static int num;
static long tim;
static Node[] id;

public static void main (String [] args)
{
    System.out.println("\nThe Original List:");
    System.out.println("-------------------\n");
    for (int i = 0; i < 50; i++)
    {
        num = storage.myRand();
        tim = storage.tellTime();
        storage.addTo(num, tim);
        System.out.println(storage);

    }
    System.out.println("\n\n");
    System.out.println("The Sorted List:");
    System.out.println("-------------------\n");

    for (int i = 0; i < 50; i++)
    {
        storage.sortNode();
        System.out.println(storage);
    }
}
}

我的输出:

The Original List:
-------------------

185     1390857365431247000      
170     1390857365431449000     
190     1390857365431511000      
157     1390857365431581000     
26      1390857365431644000      
111     1390857365431724000     
198     1390857365431785000      
116     1390857365431849000     
180     1390857365431912000      
131     1390857365431977000     
57      1390857365432069000      
55      1390857365432169000     
43      1390857365432231000      
79      1390857365432296000     
50      1390857365432357000      
19      1390857365432417000     
171     1390857365432481000      
150     1390857365432541000     
138     1390857365432607000      
48      1390857365432668000     
28      1390857365432732000      
178     1390857365432792000     
37      1390857365432855000      
27      1390857365432915000     
98      1390857365432978000      
161     1390857365433038000     
34      1390857365433102000      
97      1390857365433161000     
169     1390857365433225000      
120     1390857365433283000     
18      1390857365433348000      
194     1390857365433457000     
124     1390857365433526000      
111     1390857365433590000     
4       1390857365433657000      
143     1390857365433719000     
138     1390857365433781000      
35      1390857365433912000     
37      1390857365433974000      
188     1390857365434039000     
42      1390857365434147000      
181     1390857365434279000     
11      1390857365434372000      
27      1390857365434442000     
174     1390857365434509000      
136     1390857365434580000     
189     1390857365434649000      
86      1390857365434778000     
110     1390857365434841000      
146     1390857365434938000

The Sorted List:
-------------------

146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000     
146     1390857365434938000      
146     1390857365434938000

谢谢!

2 个答案:

答案 0 :(得分:1)

代码和解决方案中存在以下问题。 您需要进行所有3次更改才能使代码生效。 否则,存在空指针异常。

1)Storage.addTo()方法

的问题

用最后插入的值替换数组中的所有元素。

请参阅:

public void addTo(int number, long time)
{
    for (int i = 0; i < id.length; i++)
    {
        id[i] = new Node(number, time);
    }
}

添加1个元素时,实际上将ID数组中的所有元素设置为等于最新节点。

这就是为什么,当你得到排序列表时,一切都是最后插入的值。

这是你应该做的一个小改动:

public void addTo(int number, long time)
{
    id[counter++] = new Node(number, time);
}

2)您的Storage.toString()方法:

应该改为迭代数组中所有节点的东西:

public String toString()
{
    String output = "";
    for(Node node: id)
    output+=node.toString();

    return output;
}

3)没有必要在for循环中的集合/类上调用toString()

随着2)的变化,您现在可以看到插入数组中的实际值。

public static void main (String [] args)
{
    System.out.println("\nThe Original List:");
    System.out.println("-------------------\n");
    for (int i = 0; i < 50; i++)
    {
        num = storage.myRand();
        tim = storage.tellTime();
        storage.addTo(num, tim);
    }

    System.out.println(storage);
    System.out.println("\n\n");
    System.out.println("The Sorted List:");
    System.out.println("-------------------\n");

    storage.sortNode();

    System.out.println(storage);
}

答案 1 :(得分:0)

toString()对象的Storage仅返回id[counter]",并且永远不会递减递减计数器。因此,您将打印最后一个元素50次。我建议让你的toString()看起来更像这样。

public String toString() {
    String s = "";
    for(int i = 0; i < id.length; i++) {
        s += i + "\t" + id[i] + "\n";
    }
    return s;
} 

您还在输出循环中包含了storage.sortNode();。您不需要对列表进行50次排序。您只需要对其进行一次排序,然后输出它。如果您修复了toString()方法,则可以完全删除for循环,使其看起来像这样。

storage.sortNode();
System.out.println(storage);

请注意,for循环完全消失了(我们将它移到了toString方法中)。