Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your actual question is actually a lot more interesting than what I answered (and harder). I've never been any good at statistitcs (and it's been a while since I did any), but intuitively, I'd say that the run-time complexity of that algorithm would probably something like an exponential. As long as the number of elements picked is small enough compared to the size of the array the collision-rate will be so small that it will be close to linear time, but at some point the number of collisions will probably grow fast and the run-time will go down the drain.</p> <p>If you want to prove this, I think you'd have to do something moderately clever with the expected number of collisions in function of the wanted number of elements. It might be possible do to by induction as well, but I think going by that route would require more cleverness than the first alternative.</p> <p>EDIT: After giving it some thought, here's my attempt:</p> <p>Given an array of <code>m</code> elements, and looking for <code>n</code> random and different elements. It is then easy to see that when we want to pick the <code>i</code>th element, the odds of picking an element we've already visited are <code>(i-1)/m</code>. This is then the expected number of collisions for that particular pick. For picking <code>n</code> elements, the expected number of collisions will be the sum of the number of expected collisions for each pick. We plug this into Wolfram Alpha (sum (i-1)/m, i=1 to n) and we get the answer <code>(n**2 - n)/2m</code>. The average number of picks for our naive algorithm is then <code>n + (n**2 - n)/2m</code>.</p> <p>Unless my memory fails me completely (which entirely possible, actually), this gives an average-case run-time <code>O(n**2)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload