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!
temphave? Where doesidcome from?