r/programming • • Sep 25 '10

Omega - Language of the Future | Lambda the Ultimate

http://lambda-the-ultimate.org/node/4088
60 Upvotes

126 comments sorted by

View all comments

Show parent comments

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

 N    QSortTime   1e7*QSortTime/(N lg N)
3e4      0.12      2.52
1e5      0.42      2.82
3e5      1.54      2.59
1e6      5.18      2.63
3e6     16.98      2.73
1e7     63.56      2.67

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.

5

u/dmhouse Sep 25 '10

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).

3

u/djimbob Sep 25 '10

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).

3

u/booch Sep 26 '10

To be fair, when saying an algorithm is O(something), you could mean best-case, worst-case, average-case, expected-case.

2

u/abeliangrape Sep 26 '10

But everyone specifies it further for best-case complexity. Though you are right about worst and average cases.

-2

u/wnoise Sep 25 '10

Fair point that it isn't in-place quicksort

Quicksort is defined to be in-place. If it's not in-place, it's not quicksort.

4

u/djimbob Sep 26 '10

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.

1

u/wnoise Sep 26 '10

The quicksort algorithm is defined by its method

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.

1

u/djimbob Sep 27 '10

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.