5 ms·
One of my less well received interview questions is: "Okay, we both know theoretical lower limits on sorting. Can you come up with a terminating algorithm that
by kabdib 15y ago
One of my less well received interview questions is: "Okay, we both know theoretical lower limits on sorting. Can you come up with a terminating algorithm that is most /pessimal/?"
Usually I get a blank stare. Sometimes I get a great answer.
- skrebbel 15y agoI think you get the blank stares because half the interviewees don't know what "pessimal" means.
- cubicle67 15y agoCmd+Ctrl+d: no entries found I guess it's a mix of optimal and pessimistic. Pessimistically optimal ("This may be optimal, but I doubt it") or Optimally pessimistic ("This is absolutely the worst possibly outcome ever") :)
- Leynos 15y agoThe latter: https://secure.wikimedia.org/wiktionary/en/wiki/pessimal https://secure.wikimedia.org/wiktionary/en/wiki/pessimal The other time I've seen a similar derivation used is in "The Story of Mel" 'Mel called the maximum time-delay locations the "most pessimum". '
- sanj 15y agoI always like the "shaking box" algorithm: 1. Check if list ordered. If it is, we're done! 2. Randomize order of list. Go back to step 1. N!
- spatulon 15y agoThis is usually known as bogosort. http://en.wikipedia.org/wiki/Bogosort http://en.wikipedia.org/wiki/Bogosort
- skimbrel 15y agoThis is more commonly known as bogosort (http://en.wikipedia.org/wiki/Bogosort http://en.wikipedia.org/wiki/Bogosort), and it's a good start for running time but isn't guaranteed to terminate. See my other comment for a link to something that goes one better than bogosort on running time.
- X-Istence 15y agoIf the shuffle is truly random eventually you will find that the list is sorted and the bogosort will terminate. If your shuffle is not truly random then it may likely never terminate.
- smosher 15y agoThat's not right. True random sources make no guarantees, and nothing says they need to be exhaustive.
- skimbrel 15y agoNot true at all. Given a uniform distribution over k discrete values (in this case permutations of the list), the expected probability of the next permutation is 1/k. This is not the same as saying that if you take k samples from the distribution, you are guaranteed to receive exactly one of each value. For any finite number n of samples, no matter how large, the probability of getting a given value tends towards 1 as n grows, but it only approaches it asymptotically. While the probability of not seeing any one value from the domain grows vanishingly small as n gets much larger than the number of possible values, it never hits zero. So even with a stochastically random source of list permutations, we cannot place a finite upper bound on a time in which bogosort is guaranteed to terminate. It gets even worse with pseudorandom number generators — these display cyclical behavior over time (any good one will take quite a while to show it, but they all will), and you might have picked a seed for your PRNG that generates a cycle that never includes the sorted permutation. So no, bogosort is not guaranteed to terminate. In practice, it has a reasonable chance of terminating for most inputs, and it will do so with an expected (not guaranteed!) runtime in O(n!) for a list of length n.
- deleted 15y ago[deleted]
- TeMPOraL 15y agoWorst case O(\inf), it's known as 'random sort' or 'bogosort'. Funny thing, it can theoretically be O(1) on quantum computers, assuming the multiple-universe interpretation of quantum mechanics: http://en.wikipedia.org/wiki/Bogosort#Quantum_bogosort http://en.wikipedia.org/wiki/Bogosort#Quantum_bogosort ;).
- Dove 15y agoMy favorite presentation (http://c2.com/cgi/wiki?QuantumBogoSort http://c2.com/cgi/wiki?QuantumBogoSort) goes approximately like this: 1. Randomize the list. 2. If the list is not sorted, destroy the universe.
- random42 15y agoIs randomizing a list of n entities once is an O(n) operation. not sure how it would be done in O(1) on quantum computers.
- TeMPOraL 15y agoThe link that Dove gave suggests there's an idea of quantum randomization that will split our universe into n! universes in constant time.
- deleted 15y ago[deleted]
- skimbrel 15y agoBogobogosort (http://www.dangermouse.net/esoteric/bogobogosort.html http://www.dangermouse.net/esoteric/bogobogosort.html) would be a good candidate except that it uses random permutation and therefore might not terminate. Maybe if you replaced the random step with a deterministic one that always yielded a not-yet-seen permutation...
- deleted 15y ago[deleted]
- biot 15y agoStart by encoding each item to sort into an expression consisting of factors of prime numbers so that it's reversible. Conway chain notation of the factors would make a nice pessimistic next step, though that might violate your termination requirement as there won't be enough Planck volumes in the known universe to represent anything beyond the first few terms. :)
- bermanoid 15y agoIf we're talking about actual sorting algorithms that aren't pathologically inefficient (i.e. if we're not allowing algorithms that deliberately make negative progress or do random stuff instead of trying to find the solution) I'd think the "pessimal" (which, TBH, I'm not 100% clear on the definition of, whether we're talking worst case runtime, worst best-case runtime, average, etc.) deterministic solution would have to be essentially to enumerate the permutation group of N elements, checking each time whether the permutation creates a sorted list or not. There are factorial(N) members in this group, which is pretty bad worst-case running time for a sort. There are many ways to enumerate permutations, and we could probably pick a terrible one there, too, especially if we use a really naive implementation without any memoization or anything like that we could probably waste all sorts of time. Bonus points for freshly generating the whole list of permutations fresh for each isSorted(applyPermutation(i,list)) test. I think to do much worse you'd probably have to start cheating, throwing in garbage calculations that are merely used to waste time, not steps that anyone would actually think of using to progress towards the solution (i.e. I could certainly dream up a nasty brute-force problem to solve for the index integers used in the inner loop, but that's in the category of deliberate time wasting) - the nice thing about this solution is that it's 100% conceivable that someone that wasn't thinking straight could come up with it and implement it just like this. Hell, a poorly written Prolog would probably come close to implementing this algorithm if the sorting problem was specified in the right (wrong?) way... There's also always SlowSort: http://c2.com/cgi/wiki?SlowSort http://c2.com/cgi/wiki?SlowSort, which works by finding the maximum element, then recursively sorting the rest of the list. That wouldn't be so terrible if it weren't for the twist: SlowSort finds each maximum element is by splitting the list into half, sorting both lists (using SlowSort, of course) to read off the maxima, and then picking the bigger one. I think this still runs faster than N!, but its runtime is guaranteed, whereas when generating permutations you might accidentally hit the right one in the first step (could always work around that by calculating all the lists first and then checking them, but again, that seems like cheating).
- sesqu 15y agoI don't like this question. Comparison- or value-based? Worst in the average or the best case? What's to stop me from mapping the inputs into, say, busy beaver space?
- kabdib 15y agoJust the fact that you object and have something interesting to say puts you ahead of ninety percent of the candidates I see. [One guy, no foolin', told a cow-orker that a byte, on an Intel platform, contained four bits. Wow.]
- sesqu 15y agoIn an actual interview situation, I'd be likely to just choke, because it's a question I can't get a good handle on. I hope you prod & probe when asking that question.
- jerf 15y agoThe word to google is "simplexity", though that gets used for other less amusing things too, so adding the word "sort" helps. Two examples: http://web.cs.wpi.edu/~dd/2223/broder_pessimal_algorithms.pdf http://web.cs.wpi.edu/~dd/2223/broder_pessimal_algorithms.pd... http://www.hermann-gruber.com/data/fun07-final.pdf http://www.hermann-gruber.com/data/fun07-final.pdf
- amalcon 15y agoWe're clearly not talking about comparison sorts here, which blows the options wide open. O(keyspace) sorts (especially a naive counting-sort) are truly terrible for short lists with large keyspaces. Absolute worst non-probabilistic for large inputs? Enumerate all possible lists given the keyspace, check if it contains the same values as the input, check if it's sorted. This is O((k^n)n!) worst-case (and I think average as well), where k is the size of the keyspace and n is the size of the input. Even if you take the obvious optimizations (enumerate only sorted lists, enumerate only lists of the correct length, etc) it's still terrible. The former improves it substantially, but not enough to finish before the heat-death of the universe on a substantial input size. The latter has no effect on the complexity. edit: Corrected the complexity. It's even worse.
- mjw 15y agoAsk a silly question, get a silly answer: for i = 0 to ackerman(list.length, list.length) pass return quicksort(list);