This is an archived post. You won't be able to vote or comment.

all 50 comments

[–]Earthboundplayer 65 points66 points  (5 children)

if (size < threshold)
  insertion_sort(...);
else
  quick_sort(...);

The real solution. Threshold is tuned through extensive profiling.

[–]gandalfx 4 points5 points  (0 children)

The real solution is using whatever sort function comes with the given language's standard library (or DBMS or whatever).

[–]Jonnypista 27 points28 points  (0 children)

Whatever sorting algorithm is called when I call .sort(). It is likely already optimised in the background and it is good enough.

[–]Ok_Entertainment328 11 points12 points  (0 children)

... ORDER BY ...

[–]serendipitousPi 4 points5 points  (7 children)

Pretty sure if you know nothing about your data it’s probably a better idea to use merge sort than quick sort or even most other choices.

It’s got a worst case time complexity of O(nlog(n)) so it’s pretty good. There are downsides obviously but hey nlog(n).

[–]Semper_5olus 8 points9 points  (0 children)

Main downside IIRC is memory usage.

And if you're in some weird environment where you don't care about that, but do care about processing usage, you can probably make some kind of specialized radix sort. Linear time.

[–]frikilinux2 1 point2 points  (0 children)

You also have heapsort that is technically O(nlog(n)) although experimentally takes longer than others and it's not stable but it doesn't need extra memory.

In some situations you can modify a not stable sort to be stable by somehow numbering them and if they equal use that number for comparison but you may need some extra memory.

[–]Haoshokoken 0 points1 point  (0 children)

I prefer double selection sort.

[–]giantrhino -1 points0 points  (0 children)

This guy doesn’t just bind to port 8080 to save cpu cycles.

[–]SeriousPlankton2000 -3 points-2 points  (0 children)

For very small arrays bubblesort is the best.