-2

What is the best algorithm to sort a link list [in C/C++]?

4

5 Answers 5

11

Merge sort is suitable for sorting linked lists. Some details here. Sample C code here.

Why is it suitable? Well, to put it simply, mergesort's main component - the merging of sorted subsequences - can be easily done on two linked lists, because all it ever needs is comparing the head elements, so it doesn't require the random access an array has.

Also, here's an article named A COMPARATIVE STUDY OF LINKED LIST SORTING ALGORITHMS you may find interesting.

1
  • I don't know why people keep linking to this site - the implementation sucks as it is overly complicated and needlessly traverses the list; the algorithm in use is equivalent to the iterative array-based version - which is not suited for linked lists as these don't allow random access - and should be replaced with a stack-based implementation; even the vanilla recursive one should perform better
    – Christoph
    Nov 14, 2009 at 9:45
6

Merge sort.

Better yet, just use std::list and its sort() method.

2
  • 1
    Would you like to elaborate the reason for merge sort. Thanks for suggesting STL, but can't use it because it is homework
    – Cory W.
    Nov 14, 2009 at 6:27
  • 1
    @Cory: of the three common algorithms (heapsort, quicksort, mergesort) it's the only one which performs well without random access
    – Christoph
    Nov 14, 2009 at 9:57
1

Depends on what you mean by "best". Do you mean quickest to implement, or most efficient runtime? The size and characteristics of what you're sorting also play a role. Sorting 10 items versus sorting 10 million items will likely lead to a different "best" algorithm. For large datasets where "best" means fastest runtime, I like quicksort.

3
  • I think I remember hearing that quicksort runs slow on sorted (or nearly sorted) data. So, as bpv said, the characteristics of the data would indeed play a large role in your decision.
    – ialm
    Nov 14, 2009 at 7:26
  • 1
    @veol: A naive quicksort runs slow, most practical implementations use randomized pivoting.
    – MAK
    Nov 14, 2009 at 7:30
  • @MAK: It seems that I wasn't paying attention in class again :)
    – ialm
    Nov 14, 2009 at 8:22
1

A stack-based mergesort implementation is the weapon of choice.

Some time ago, I needed to sort a doubly-linked list. Wikipedia told me to use mergesort as it's the only one of the three common general-purpose sorting algorithms (heapsort, mergesort and quicksort) which performs well when random-access is slow and even provided a link to Simon Tatham's implementation.

First, I re-implemented his algorithm, which showed that it was basically the well-known iterative version. It's a bad choice as it relies on random access and therefore performs poorly.

The recursive version is better suited for linked lists as it only needlessly traverses one of the lists when splitting and not both (as does Simon's variant).

What you should use is a stack-based version as this implementation gets entirely rid of unnecessary list traversals.

0

It's worth thinking about how you got to this point. If you're going to need your items sorted in one particular way, consider keeping the list sorted and inserting them directly into the sorted list. That way you don't have to sort it when you need it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.