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

Posted by Carl on Stack Overflow See other posts from Stack Overflow or by Carl
Published on 2008-12-17T17:34:00Z Indexed on 2010/05/06 2:18 UTC
Read the original article Hit count: 263

Filed under:
|
|

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:

bool[] bIndexSet = new bool[iItemCount];

for (int iCurIndex = 0; iCurIndex < iItemCount; iCurIndex++)
{
    int iSwapIndex = random.Next(iItemCount);
    if (!bIndexSet[iSwapIndex] && iSwapIndex != iCurIndex)
    {
        int iTemp = values[iSwapIndex];
        values[iSwapIndex] = values[iCurIndex];
        values[iCurIndex] = values[iSwapIndex];
        bIndexSet[iCurIndex] = true;
        bIndexSet[iSwapIndex] = true;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about shuffle