random sort
Moderator: Stef
random sort
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?
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random sort
There is no "random sort" feature in SGDK but you can use that kind of algo:
not really straightforward but at least it works
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];
}
-
- Very interested
- Posts: 117
- Joined: Sun Jan 02, 2011 9:14 pm
- Contact:
Re: random sort
ShuffleVector_u8(...) https://github.com/moon-watcher/BugHunt ... chos.c#L78
By Jack Nolddor
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] );
}
}
Re: random sort
Thank you both!
-
- Very interested
- Posts: 3131
- Joined: Thu Nov 30, 2006 9:46 pm
- Location: France - Sevres
- Contact:
Re: random sort
Haha the algo from Moon is much more simple, i suggest you to use it Crazy that i even didn't think about just shuffling the array :p