I've been tinkering with various structures such as binary trees and linked lists (creating my own, not using built in or library functions/objects) because I thought those would be good things to have experience with. This question is about sorting a linked list from smallest to largest. This is the basic setup I was using but the list basically fell apart when I tried to sort it and I have no idea why.
Here is an example list of objects, each with a value (the last object has a value of 4, and there is no next object):
1 -> 3 -> 5 -> 2 -> 4 -> null
I do this by starting at the first object (value of 1) and looking for the object with the lowest value that comes after it (and is lower than it). That would not result in anything so it would move on to the second object (value of 3) and look for the lowest value (after it and that is lower than it) which would be object 4 (value 2). It would then switch the second and fourth objects resulting in this list:
1 -> 2 -> 5 -> 3 -> 4 -> null
next:
1 -> 2 -> 3 -> 5 -> 4 -> null
finally:
1 -> 2 -> 3 -> 4 -> 5 -> null
The problem comes in switching those objects, all the references to them, and from them.
This is what I would do for the first switch (as shown above):
Object #1 (value 1) would refer to Object #4 (value 2)
Object #3 (value 5) would refer to Object #2 (value 3)
That switches the objects, but now the list is not continuous so this needs to be done:
Object #2 (value 3) would refer to Object #5 (value 4)
Object #4 (value 2) would refer to Object #3 (value 5)
I use temporary variables to do this, as we all should know doing this:
a = b
b = a
will not switch their values, a temporary variable is needed.
This seems like a sound way to do it to me, is there some huge hole that I'm missing or am I overcomplicating it in some way?
ID:148859
Sep 1 2002, 11:06 pm
|
|
In response to ACWraith
|
|
Well, it's nice to know I'm not going crazy ;)
I guess I must have some little thing that isn't right in the code, but at least the method should work. I should probably use a better sorting algorithm, in general there shouldn't be more than 6-10 items in the list (which isn't too terribly bad) but there really isn't any limit on how high it could go. Maybe I'll use a binary tree, the values can be fed in such a way that the tree should be fairly balanced without any manipulation necessary. Of course then I have to deal more with recursion which can be a resource suck, but I suppose that's the way it goes :p Or I could used a two way linked list where I sort it as I add the data which wouldn't be quite as good as a binary tree but I wouldn't have to deal with recursion. Gah, too many options! :p |
In response to English
|
|
If you are worried about actual performance, use the built-in /list. They are binary trees internally and can have over roughly 4 billion more members. You won't get the linked list experience, but can still get experience creating the sorting algorithms.
|
It's BubbleSort and runs in O(n*n) time. It's not a nice algorithm for lists with many members (large n), but it gets the job done for small lists.