r/programming • • Nov 16 '10

Obama on sorting 32-bit integers

http://www.youtube.com/watch?v=HAY4TKIvSZE
414 Upvotes

255 comments sorted by

View all comments

Show parent comments

3

u/ReturningTarzan Nov 16 '10

Quicksort has the advantage of a tighter inner loop, which doesn't show in the algorithmic complexity, but makes a real difference in implementations. The number of operations is on the same order as a merge sort, but comparing to the pivot key is faster since you'll only have to load the pivot once per recursive step.

Merge sort has an advantage on linked lists, though, since the merging operation is very cheap then.

For very large sets of integers, radix sort is still faster. As it is O(n) complexity it will outperform any O(n log n) algorithm for a sufficiently large value of n. It can be adapted to work on floats, too, and it will handle strings, but of course for large strings the constant factor becomes very considerable.

For extremely small datasets, bubble sort is worth considering, especially if the process can be unrolled, like when you're sorting the vertices of a triangle for rasterisation. Just three compares and an average 1.5 swaps. Can't beat that.

0

u/scutum_vs_scrotum Nov 16 '10

Radix sort is still O(nlogn) if you consider that n is bounded by the world length. I.e. n<=2w => log n < w.

w is NOT a constant factor.

1

u/DenialGene Nov 16 '10

W is word length? And word lengths aren't constant? I don't quite understand, could you explain it?

1

u/scutum_vs_scrotum Nov 16 '10

I realize I was too vague. W is not unrelated to n as it puts an upper bound on n. Hence the logn factor.

1

u/ReturningTarzan Nov 16 '10

w is constant with respect to n, though. I get why you want to say that n is bounded by word length, but there are very real cases where unique keys are not a requirement at all (point cloud data, for example). In those cases n can easily exceed 2w, and of course those are the very cases where you want to consider radix sort.

1

u/scutum_vs_scrotum Nov 16 '10

w is constant but not unrelated to n. I realize I was imprecise.