简体   繁体   中英

How Do I Sort an Array of Objects using Insertion Sort?

Can someone please shed some light on why my insertion sort is not working? You don't have to write the code for me. Just lead me in the direction I need to go.

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);
    }
}
}

My Output:

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

Thanks!

There are the following problems in the code, and solutions. You need to make all 3 changes to get the code to work. Otherwise, there are null pointer exceptions.

1) Issue with Storage.addTo() method

Your replacing all elements in the array with the last inserted value.

See:

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

When you add 1 element, you actually set ALL the elements in the ID array to be equal to the latest node.

That is why, at the end when you get the sorted list everything is the last inserted value.

This is One of the small changes you should make:

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

2) Your Storage.toString() method:

Should by changed to something iterating over all nodes in the array:

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

    return output;
}

3) It is not necessary to call toString() on a collection / class within the for loop.

Along with the change in 2) , you now see the actual values inserted in the array.

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);
}

Your toString() for your Storage object only returns id[counter]" and you never increment of decrement counter. Therefore, you will print the last element 50 times. I suggest making your toString() look more like this.

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

You also included storage.sortNode(); inside of your output loop. You don't need to sort your list 50 times. You only need to sort it once, and then output it. If you fixed your toString() method, you can just remove that for loop completely to look like this.

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

Notice that the for loop is completely gone (we moved it inside of the toString method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM