Choosing k out of n

前端 未结 3 453
心在旅途
心在旅途 2021-02-04 01:45

I want to choose k elements uniformly at random out of a possible n without choosing the same number twice. There are two trivial approaches to this.

3条回答
  •  遥遥无期
    2021-02-04 02:23

    Your second approach does not take Theta(k log k) time on average, it takes about n/(n-k+1) + n/(n-k+2) + ... + n/n operations, which is less than k(n/(n-k)) since you have k terms which are each smaller than n/(n-k). For k <= n/2, it takes under 2*k operations on average. For k>n/2, you can choose a random subset of size n-k, and take the complement. So, this is already an O(k) average time and space algorithm.

提交回复
热议问题