Sorting a Linked List

Started by
17 comments, last by MrChrisnis 11 years, 7 months ago
Sounds like a job for mergesort. You'll learn a lot about sorting algorithms just from studying one at a time. You've stumbled into an interesting area of computer science. Have fun!
Advertisement
Thanks for all the replies so far guys. Taking a little break because I've been staring at this stuff for a while now and it's just like *boom, head explode* tongue.png

My list relies on the user adding the data in themselves, therefore there is nothing in the list to start with, so would it be better for me to amend my add/add_to_end functions to sort the objects as they are added in?

My list replies on the user adding the data in themselves, so would it be better for me to amend my add/add_to_end functions to sort the objects as they are added in?

That would be simpler. If you need to sort multiple times (that is, if you have multiple lists you want to sort), then writing a sort function that can be reused would make sense. But I'd go with what you suggested there if that's all you need to do.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Whatever language you are using there should be a standard library for sorting stuff so that you don't have to write your own.

As for sorting on multiple attributes, the best way is to do it in one call but use a compare function like this (pseudocode):

int node_diff(node1, node2) {
int diff = string_diff(node1.name, node2.name);
if (diff == 0)
diff = node1.health - node2.health;
return diff;
}

where diff < 0 indicates less-than, == 0 for equal-to, and >0 for greater-than.

openwar - the real-time tactical war-game platform


My list relies on the user adding the data in themselves, therefore there is nothing in the list to start with, so would it be better for me to amend my add/add_to_end functions to sort the objects as they are added in?


In this case, you should consider storing the data in a binary search tree instead of a linked list because if you insert into a binary search tree you get sorted-ness for free. I don't know what language you are working in but in C++ i'm talking about using either an std::set or an std::map.

However, i don't know what you are doing so it might make sense to use a linked list maintained as ordered if you actually need list functionality such as O(1) unordered inserts or O(1) splicing of two lists together, etc.
Optimizing (if the circumstances allow it)

If the data DOESNT change as much as the sorted list gets displayed, then retaining the Sort results data between its use (only resorting when the list changes).

Then it might even be possible to optimize by extracting each individual changed item from the Sort data and re-insert it into that previous Sort-Result (eliminating a full/complete resort every time) -- only if a few change.

For data sets that the majority of data constantly change, you will have to rebuild the sorted results every time you used them (display them)

----

Another consideration for the actual Sort methodology is how big is the set of items being sorted. Some more complicated Sort methodology (which may be efficient for large data sets) may have alot of overhead as compared to a simpler more brute-force Sort done on a small set. Sometimes you can even check the data set size at run-time and have a decision made between two methods (after having off-line tested to see where the crossover point is with typical data set size ranges)

----

I would use a Binary Tree Sort for large random data sets (the sorted tree data is seperate from the 'link list' so can be a different data type - though I have frequently had both the link list chain data AND seperate tree Sort data elements in the objects 'record' struct.

More esoteric :

For some huge data sets, cache hit/collision issues with some sorts become important for game speed and you want the Sort manipulation data to be as tiny as possible (possibly one of those "Structure of Arrays" kind of things for permanent object data)
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Okay, I've tried implementing my own insertion sorting after writing it out, and it is still confusing me. This causes the program to pretty much break after one Beast has been inserted into the list. It doesn't break out or crash or anything like that, it just stops outputting the command line requests that it is supposed to and stop allowing the user to enter in any more inputs.

If you guys could look through it and give me some hints on how I can fix this it would be great smile.png

Thanks,
Chris

P.S - I have tried! sad.png

EDIT: My code is displaying in this post as though it is on one long line. I am not sure how to fix this or if it is showing like this for other people so I am sorry if it is
Looks like it's because of line endings.

Enjoy it ;)


void SimpleLinkedList::add_numerical(Beast *b)
{
if(head == NULL)
{
head = b;
return;
}
Beast *previous = head;
Beast *next = previous->get_next();
while(next != NULL)
{
if(head->get_HP() < b->get_HP())
{
next = head;
head = b;
b->set_next(next);
}
if(next->get_HP() get_HP()) -- //wtf?
{
previous->set_next(b);
next = next->get_next();
}
if(next->get_HP() > b->get_HP())
{
next = next->get_next();
}
}
}


I suggest you to write abstract Linked list such it's in STL and then inherits custom list of it.
C x 2 C x o C x C f nice C x C s C x C c
Hmm, that mistake isn't in my code =S

Copy and paste fail ftw.

I fixed the problem I had (Needed a return call added after b->set_next(next); in the first if statement), and have run into another. Time to go try to figure that out.

And I have to do it this way as it is an assignment that I am working on and my lecturer wants it done this way :(

This topic is closed to new replies.

Advertisement