0

In worst case R-select is O(n^2) where as select is O(n). Can someone explain and contrast their behavior in average cases.

P.s. - I am not sure if its a repetitive question. I can delete if its the case! Thanks!!

polyglot
  • 57
  • 11
  • P.P.s.: Please let me know if additional information is required around the question! . Thanks! – polyglot Oct 23 '16 at 06:27
  • Forgive my ignorance, what is "R-Select"? – Henry Oct 23 '16 at 06:29
  • IMHO, If the goal were to find the ith smallest element in a array (without changing any order of the elements) we could randomly choose a pivot to achieve this. This would be R-select. Hope this makes my question more comprehensible. thanks! – polyglot Oct 23 '16 at 06:44

1 Answers1

0

By R-select, I'm assuming you're talking about the randomized selection algorithm that works by choosing a pivot, partitioning on that pivot, and recursively proceeding from there. If that's not the case, let me know!

You're correct that the R-select algorithm's worst-case is Θ(n2), but that's extremely unlikely to arise in practice. This requires you to very frequently pick a pivot that's within a constant number of elements away from the min or max value, and the likelihood that this occurs is exponentially low. The average-case runtime of O(n) is actually quite likely to occur; you can prove, for example, that for any constant k, the probability that the runtime is O(n log n) is at least 1 - 1/nk.

The constant term hidden in the big-O notation of R-select is actually very low, so low in fact that R-select is typically much, much faster than the median-of-medians selection algorithm. In fact, they're sometimes combined together. The introselect algorithm works by running R-select and looking at the runtime, switching to the median-of-medians selection algorithm in the event that the runtime ends up looking bad. The overall runtime is then worst-case O(n) and comparable to R-select.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065