Most efficient way to randomly “sort” (Shuffle) a list of integers in C#

前端 未结 12 1325
长发绾君心
长发绾君心 2020-11-22 12:25

I need to randomly \'sort\' a list of integers (0-1999) in the most efficient way possible. Any ideas?

Currently, I am doing something like this:

bo         


        
12条回答
  •  悲&欢浪女
    2020-11-22 13:05

    To improve your efficiency you can keep a set of values/indices that have been swapped rather than a boolean for indicating they were swapped. Pick your randomized swap index from the remaining pool. When the pool is 0, or when you made it through the initial list then you are done. You don't have the potential to try to select a random swap index value.

    When you do a swap, just remove them from the pool.

    For the size of data you are looking at it is no big deal.

提交回复
热议问题