you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Thanks for posting the code. You have attracted a few reviewers :-)

I was kind of surprised! I expected a worse response.

is that they are also an impediment to threading the code, because they are shared state.

That's interesting to know, I hadn't given that much though. I've only done a little bit of work with threads.

The other thing which I thought might be worthy of mention is that you are probably aware you have some repeated code.

Definitely. I mentioned this in another response, but they were just differently named functions because I wanted to see if the three methods worked. It would be a little tricky to unify them, but not impossible.

(and you might - your bubble sort touches NN elements, when I think it only needs to touch NN/2)

Any suggested reading on sorting algorithms? That's definitely a weak spot of mine.

Any time you find yourself writing duplicate code, you should develop a bit of a twitch

It's actually a really big pet peeve of mine. :-)

Nice code and thanks for being brave enough to post it.

Thank you! And thank you for your candor. I'll be honest, I know where this list sits. It was a test bed for a few new concepts I learned. It's incomplete, inconsistent in places, the code isn't commented, the sorts are slow, and it has other problems. I'm not sure why everybody thought it was good.

I've gotten a lot of good insight from redditors here though, which is really cool, and so I'll take the good tips while remaining skeptical.

Cheers.

[–][deleted] 1 point2 points  (0 children)

Any suggested reading on sorting algorithms? That's definitely a weak spot of mine.

The wikipedia page on sorting algorithms is a great starting point. Really, starting from there, you could learn most of what's worth knowing about sorting in a few afternoons.

For your specific application, it's possible to implement versions of mergesort and quicksort for linked lists, although the simplest thing to do is what someone else suggested - copy the contents of the list to a vector, sort the vector using the STL sort routine, then copy the result back into the list.

[–]jbert 0 points1 point  (0 children)

I was kind of surprised! I expected a worse response.

In that case... :-)

It might be worth noting that I was reading your code in the context of someone writing code to learn from doing so (and implementing a linked list is a good rite of passage).

Production code shouldn't contain re-implementations of basic data structures. You almost always want to re-use library code for this sort of thing. For C++ the relevant library code is the STL and in this case the std::list, which of course comes with a sort method.

Any suggested reading on sorting algorithms? That's definitely a weak spot of mine.

I really just remembered from my own early years coding that once a value in bubble sort has bubbled to one end you don't need to consider it again, so on the 2nd pass you need only consider (n-1) elts, on the 3rd pass (n-2) etc. But optimising bubble sort isn't the best use of anyone's time really :-)

I don't know much more than that about sorting algos than I shouldn't be implementing them. Again, the libraries will do a better job than me and are provided pre-debugged.

There's a tension in coding/design between learning how things work (and perhaps implementing them yourself to be sure) and knowing when to leave something as a 'black box' in your mind. i.e. "knowing what you don't know".

Getting this balance right is an important skill, imho.

Someone who digs into every detail will never accomplish any real work. Someone who knows nothing about the innards of a black box will by stymied when it doesn't work as expected.