Fair point that it isn't in-place quicksort or the fastest quicksort. But it is still the quicksort algorithm, and still is O(N lg N) complexity. I would hesitate to say I was being ironic or flat-out incorrect.
Again, to verify its still O(N lg N), I profiled times for it sorting sorting N random numbers.
The last column being roughly constant demonstrates its still scaling as O(N lg N). (Yes there are other O(N lg N) sorting algorithms, but this uses the logic of quicksort and still scales as quicksort does. No its not optimal, but its still quicksort.
Technical nitpick: quicksort actually isn't O(n log n). If you use it on a sorted list of n elements it takes O(n2) time. However, it is expected-case O(n log n).
Agree, though I was sorting random numbers so with overwhelming probability I expect roughly O(n lg n).
Using a random pivot gets rid of the O(n2) behavior on a sorted or near-sorted list, which is could . (But even in with the random pivots you still get an amortized time of O(n2) if your choice of random pivots was in the order that sorts the list; but that should occur only 1 out of n! ~ (n/e)n times).
The quicksort algorithm is defined by its method -- being a recursive divide-and-conquer sorting algorithm that takes a list compares elements to a pivot moving all variables less than the pivot to one side of the pivot and all variables greater than the pivot to the other side. How it stores the values in memory is irrelevant to the algorithm. Yes, one of the benefits about the algorithm is that it can be done in-place in languages with mutable variables. However, pure functional programming languages like haskell or Ωmega require all variables to be immutable, preventing any in-place sort (unless you write a custom mutable array, which gets rid of variable persistence, the main benefit of using pure functional programming).
The problems people had with my algorithm (that is equivalent to the classic haskell example of quicksort) is (a) it is about half the speed of other haskell qsort algortihms as it does two sets of comparisons each time through (finds all elements in xs that are less x and then starting from scratch find all elements in xs that are greater or equal to x), and (b) it fails spectacularly if there's is an element that's neither less than or greater or equal to the number its compared to (e.g., 0/0=NaN) while other algorithms don't.
I don't disagree with those disadvantages; I would disagree with saying its incorrect or not quicksort, even though I agree it could be improved by doing only one filter through the list into two halfs (which would also handle NaN's more reasonably). However, that is slightly less clear pedagogically.
That's exactly what I'm saying: the implementation matters. Partitioning-and-recurse-on-both-portions is a handy summary of quicksort, but that's not what quicksort is. Quicksort very specifically does not allocate extra space for elements (only indices on the stack frames).
Quicksort is also specifically an array sort, not a list sort.
There's also the tiny issue that laziness turns the execution order nearly inside-out.
My view is "quicksort" refers to the algorithm independent of implementation; feel free to have an alternative viewpoint, but this view is fairly common and language evolves. I could quicksort or bubble sort a set of physical books, or (inefficiently) quicksort a list of unsorted names by writing them down consecutively lists going through the iterations on paper, as I go through the algorithm.
Quicksort is not limited to arrays. While it is particularly efficient on arrays, there's no problem sorting lists (like a C++ STL vector or pythonic lists) of variable length strings (compare what's being pointed to, move the pointers) that consist of arrays of pointers. Even linked lists can be quicksorted easily though possibly less optimal than other sorts (due to O(N) cost of choosing a non-first item as a pivot, which will make list possibly O(N2) efficient if it starts nearly sorted). I used the word list rather than array as it is more generic. (An array is always a list; a list is not always an array).
Hoare's original quicksort paper talks about sorting items (never using the word array). Wikipedia refers to sorting arrays, lists, and linked-lists. Hoare does refer to the fact that the algorithm can be done in place in memory, but to me that's more of a benefit of quicksort rather than its defining quality.
8
u/djimbob Sep 25 '10
Fair point that it isn't in-place quicksort or the fastest quicksort. But it is still the quicksort algorithm, and still is O(N lg N) complexity. I would hesitate to say I was being ironic or flat-out incorrect.
Again, to verify its still O(N lg N), I profiled times for it sorting sorting N random numbers.
Using the ghc profiler and changing N
The last column being roughly constant demonstrates its still scaling as O(N lg N). (Yes there are other O(N lg N) sorting algorithms, but this uses the logic of quicksort and still scales as quicksort does. No its not optimal, but its still quicksort.