News for hackers. Stuff that matters.
Re:Illustrated Sorting Algorithms by TheLoneWolfling on Saturday April 26, 2014 @05:17PM (#7652782)
I wish it had a "pathological case" option for data to sort per-algorithm.

In particular: Quicksort, which (unless you're willing to make an (expensive) call to a (P)RNG per iteration) has a worst case of O(n^(2)) time.

Re:Illustrated Sorting Algorithms by TwoBit on Saturday April 26, 2014 @07:15PM (#7653110)
Despite that no real-world quicksort implementations have that worst case (due to fallbacks to other sorts), it seems like it's useful to see the pathological case of pure quicksort anyway.
Re:Illustrated Sorting Algorithms by TheLoneWolfling on Saturday April 26, 2014 @07:16PM (#7653121)
At which case: why bother using quicksort?
Re:Illustrated Sorting Algorithms by jemfinch on Saturday April 26, 2014 @11:07PM (#7653743)
Because in the real world, where all our code must actually run, quicksort beats practically every other algorithm on the vast majority of inputs. Its cache behavior is better than heap sort, its memory usage is lower than merge sort, and even its O(log n) memory usage is efficiently allocated on the stack as opposed to hitting a slow system allocator.

"Engineering a Sort Function"[0] covers this quite well.

In some experiments of my own, I've found that even Smoothsort[1] (an algorithm specifically designed to have a smooth transition from linear to linearithmic behavior on nearly-sorted input) can't beat the C++ system's introsort (a quicksort variation with a fallback to heapsort) in already-sorted inputs.

[0] http://www.skidmore.edu/~meckmann/2009Spring/cs206/papers/sp...

[1] http://www.keithschwarz.com/smoothsort/

Re:Illustrated Sorting Algorithms by mjcohen on Saturday April 26, 2014 @09:11PM (#7653452)
It's simple(relatively) and almost always quite fast. Or would you rather use slowsort?
Re:Illustrated Sorting Algorithms by TheLoneWolfling on Saturday April 26, 2014 @09:36PM (#7653526)
Quicksort is not simple if you have to build in an entire other sorting algorithm as a failsafe.

And merge sort is often faster, especially if the comparison function is relatively expensive.

Re:Illustrated Sorting Algorithms by RogerL on Saturday April 26, 2014 @10:52PM (#7653710)
Yes, sometimes, but it also uses more memory. In practice, with real problems, quicksort wins with in-memory sorting, and mergesort wins with too big for memory data sets.
Re:Illustrated Sorting Algorithms by robertnn on Saturday April 26, 2014 @06:22PM (#7652983)
Why would the worst case change if you use a random number for choosing the pivot?
Re:Illustrated Sorting Algorithms by TheLoneWolfling on Saturday April 26, 2014 @07:15PM (#7653116)
Ah.

Technically, the worst case stays the same.

In actuality, the worst case occurring via random chance is not a problem, as it occurs vanishingly infrequently. The problem is malicious input.