Skip to content

Instantly share code, notes, and snippets.

@fbarrios
Created October 30, 2017 01:23
Show Gist options
  • Save fbarrios/11d71b3a7e2c0003f77c41a056718a75 to your computer and use it in GitHub Desktop.
Save fbarrios/11d71b3a7e2c0003f77c41a056718a75 to your computer and use it in GitHub Desktop.
#include <time.h>
#include <stdlib.h>
...
static void swap(void* array[], size_t p1, size_t p2)
{
void* temp = array[p1];
array[p1] = array[p2];
array[p2] = temp;
}
static void prueba_abb_volumen(size_t largo, bool debug)
{
const size_t largo_clave = 10;
char* claves[largo];
unsigned* valores[largo];
for (unsigned i = 0; i < largo; i++)
{
claves[i] = malloc(largo_clave);
valores[i] = malloc(sizeof(unsigned));
sprintf(claves[i], "%08d", i);
*valores[i] = i;
}
srand((unsigned int) time(NULL));
// Mezclamos el array: algoritmo de Fisher–Yates
for(int i = (int) (largo - 1); i >= 0; i--)
{
unsigned int j = rand() % (i+1);
swap((void**) claves, i, j);
}
...
// ¡Recordar liberar las claves!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment