Results 1 to 4 of 4
- 11-27-2010, 05:33 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Insertion Sort for linked list help?
Hi, I'm writing an Insertion sort for linked lists and I have the sort working but if I add certain numbers to the list it won't work. I cannot figure out what could be causing this error. It sorts [4, 2, 1, 5, 3, 10, 6, 11, 7] perfectly, but if I add a 9 to the end or other numbers it doesn't work:confused: Any help would be g
[/CODE]Last edited by bubtub24; 11-28-2010 at 06:05 AM.
- 11-27-2010, 09:45 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
I have the sort working but if I add certain numbers to the list it won't work
It's time to debug!
The thing is to see what it is that your program is doing. IDE's offer debuggers, or you can use the poor man's debugger: System.out.println(). (Actually the latter has some advantage because others here can see your code and run it.)
Java Code:/** * Returns the list from first onwards * as a string of info values. */ static String asString() { StringBuilder buf = new StringBuilder(); for(Node n = first; n != null; n = n.link) { buf.append(n.info).append(" "); } return buf.toString(); } // ... public static <T> void insertSort() { Node<Integer> current, head, trail; current = first; trail = current; head = current.link; while(current != null) { System.out.println("head=" + head.info); System.out.println("trail=" + trail.info); System.out.println("current=" + current.info); if(head.info < trail.info) { System.out.print("Found " + head.info + " out of order: "); int temp = head.info; deleteNode(temp); position(temp); } else { System.out.print(head.info + " OK: "); } System.out.println(asString() + "\n"); trail = current; head = current.link; current = current.link; } }
Basically you are looking to see at what point the head, trail, and current values are not what you expect them to be.Last edited by pbrockway2; 11-27-2010 at 09:48 PM.
- 11-28-2010, 05:54 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Thanks for the information it was the trail and head that were wrong. It needed a small amount of tweaking now is fixed.:)
- 11-28-2010, 06:21 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
problem with insertion sort???
By blueduiker in forum New To JavaReplies: 2Last Post: 03-22-2010, 01:17 PM -
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
[SOLVED] Insertion Sort in Linked List
By taylorp in forum New To JavaReplies: 10Last Post: 03-27-2009, 12:34 AM -
Insertion Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:41 PM -
Insertion sort algorithm
By Albert in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 08:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks