random sort
Posted: Mon Sep 12, 2016 5:47 pm
I've an array of numbers 1 to 20 and i'd like to randomly sort them
What's the best sort to use with the SGDK?

Code: Select all
array[20] = {0, 1, 2, ..., 19}
remaining = 20
outInd = 0
while (remaining > 0)
{
int index = random() % remaining;
value = array[index];
yourArray[outInd++] = value;
remaining--;
array[index] = array[remaining];
}
Code: Select all
static void ShuffleVector_u8(u8 *array, u32 n)
{
u32 i;
for (i = 0; i < n ; i++) /* RECORREMOS UNO A UNO EL VECTOR... */
{
u32 j = random() % n; /* ... Y LO INTERCAMBIAMOS CON OTRO ÍNDICE AL AZAR */
SWAP( array[i], array[j] );
}
}