Return to Snippet

Revision: 55880
at February 26, 2012 22:23 by fixpq


Initial Code
public  static void Shuffle<T>(T[] array)
    {
        Random random = new Random();

        for (int i = 0; i < 10; i++)
        {
            int idx = random.Next(i, 10);
            
            //swap elements
            T tmp = array[i];
            array[i] = array[idx];
            array[idx] = tmp;
        }  
    }

Initial URL

                                

Initial Description
This method shuffles an array. This is the optimum implementation for such an algorithm -  O(n).

Initial Title
Shuffle / randomize array

Initial Tags
array

Initial Language
C#