Live data from Hacker News

Lampsort: Leslie Lamport's Non-recursive Quicksort

bertrandmeyer.com

41–46 of 46 posts

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#41
post #35
post #32

Earlier quoted context omitted.

That's a good observation. It should go without saying that recursion can always be expressed iteratively. Lamport is inclined to make observations like this, e.g. his sequential consistency concept which states that concurrency is always equivalent to a serial process. But don't you mean "multiple recursive calls", not specifically two?

> But don't you mean "multiple recursive calls", not specifically two? Yes, of course. Exercise for the non-lazy lurker: can you generalize the Quicksort algorithm based on this? Is there any benefit to doing so?

You can if the partition uses new storage rather than swapping in-place (which is likely what you want anyway for horizontal parallelism, see below): pick multiple pivots and insert elements into a bucket depending on how many pivots they're greater than. This turns out to be called samplesort, with some extra detail regarding picking the buckets.

However, once you give up on in-place you should probably use radix sort instead, as long as your elements, or their hashes, have some semblance of uniform distribution: this avoids having to calculate greater-than on a large number of pivots, although depending on the architecture it may be possible to do the latter reasonably quickly, and I suppose you may end up saturating memory bandwidth anyway...

Anyway, you can run a single partition in parallel - just not in place - by dividing the input data and having separate output queues per core. You probably need to merge the buckets back into the original array anyway, so this doesn't hurt; even if you want to keep the buckets separated (but individually contiguous) for some reason, e.g. if you don't need the final output as an array and want to save some difficult-to-parallelize merging, it just makes the partition a bit trickier with some atomic stuff.

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#42
post #22
post #10

Earlier quoted context omitted.

I think it probably has a lot to do with the fact that the author, Bertrand Meyer, invented Eiffel ( http://en.wikipedia.org/wiki/Bertrand_Meyer#Computer_languag... ).

Eiffel is considered by some people to be one of the better programming languages (maybe even near the top of the heap, though of course a lot depends on application area you are using it for, just as with any other language). Features in it may have influenced other languages. Not sure, but I think Design by Contract is one of them - preconditions, postconditions and invariants, which can be implemented somewhat, us…

Better is always subjective. Eiffel is not exactly in common usage in real programs. Lots of people think their language is better at something but until lots of people start using it, we don't really know.

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#43
post #28

TL;DR: if you have an algorithm that makes two recursive calls and it doesn't matter what order you do them in (e.g. Quicksort) then you can rewrite that algorithm to use a set of sub-problems in stead of a stack of sub-problems. The latter is what you (implicitly) get when you write an algorithm recursively. A much more interesting observation would have been to note that you could accomplish the same thing by intro…

"you could accomplish the same thing by introducing a new language construct that explicitly noted that the order of two calls doesn't matter" Effectively, this is what Golang's "go" construct does. Combined with channels you can construct an arbitrary dependency graph as well.

A very common need is to do an bunch of independent RPCs. Small wish: Allow returning values from a goroutine, along the lines of "results <- go rpc(args)".

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#44
post #17

I hiss at the term "non-recursive quicksort" which is used a lot. Quicksort per definition is recursive logically. The same computation recurs in the subarrays, you know. Whether you maintain the state by using the programming language's own stack, a separate stack, a queue, or some other data structure either implicitly or explicitly is a matter of implementation. The amount of space you need to allocate for the pro…

"The same computation recurs" Hmm...you mean like in a loop?

Nope.

You can run, for example, bubble sort or insertion sort in a loop, always iterating over the same set of data (with some fixed amount of state) until the array is sorted.

Conversely and per definition, quicksort divides the data flow into subarrays and then sub-subarrays recursively which means that a plain single loop with constant space for state won't do. You need extra space for storing state and the amount of that extra space needed depends on the size of the input; logarithmically, in quicksort's case.

The implementation of recursion can vary but the control and data flow still does recur in a nested fashion.

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#45

Earlier quoted context omitted.

Not quite. Naive recursion doesn't generally change the order between the two subproblems at a single stage. It's generally just sort(first half), sort(second half). Think of the case of a really bad pivot as the first choice - say, the second-highest element. Naive recursion generally will recurse on the lower chunk first, whereas smallest chunk first will recurse on the higher chunk.

Fair point. I assume you get a reasonable pivot. Recursing on one of the two halves of the "current" partition is the cache friendliest option. Smallest interval will guarantee this by induction, I think, but I've never heard that the size of the stack is really a problem in quicksort. If you continuously partition out only a single element, your runtime is going to suck, even if you "sort" those elements first.

> I've never heard that the size of the stack is really a problem in quicksort

Naive quicksort tends to, in pathological cases, run out of stack space as opposed to having performance issues.

Smallest-interval ends up with a strict upper limit of O(log n) elements in the running set, as opposed to O(n) for naive quicksort. This can be useful.

Re: Lampsort: Leslie Lamport's Non-recursive Quicksort

#46
post #22

Earlier quoted context omitted.

Eiffel is considered by some people to be one of the better programming languages (maybe even near the top of the heap, though of course a lot depends on application area you are using it for, just as with any other language). Features in it may have influenced other languages. Not sure, but I think Design by Contract is one of them - preconditions, postconditions and invariants, which can be implemented somewhat, us…

Better is always subjective. Eiffel is not exactly in common usage in real programs. Lots of people think their language is better at something but until lots of people start using it, we don't really know.

>Better is always subjective. True, and I implied that in my first sentence. It is well known that Eiffel is not used by a lot of people. That does not automatically mean it is not good, though.
Post reply on HN