无序排列/随机化数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无序排列/随机化数组相关的知识,希望对你有一定的参考价值。

This method shuffles an array. This is the optimum implementation for such an algorithm - O(n).
  1. public static void Shuffle<T>(T[] array)
  2. {
  3. Random random = new Random();
  4.  
  5. for (int i = 0; i < 10; i++)
  6. {
  7. int idx = random.Next(i, 10);
  8.  
  9. //swap elements
  10. T tmp = array[i];
  11. array[i] = array[idx];
  12. array[idx] = tmp;
  13. }
  14. }

以上是关于无序排列/随机化数组的主要内容,如果未能解决你的问题,请参考以下文章