Skip to content

Instantly share code, notes, and snippets.

@guidanoli
Created August 21, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidanoli/5b0b428e585fd46f432c8311b4b012c8 to your computer and use it in GitHub Desktop.
Save guidanoli/5b0b428e585fd46f432c8311b4b012c8 to your computer and use it in GitHub Desktop.
Array permutation
/*
* perm.c
*
* Array permutation
*/
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
assert(argc == 2);
int n = atoi(argv[1]);
assert(n != 0);
srand(time(NULL));
unsigned int *array = malloc(sizeof(int)*n);
assert(array);
for (int i = 0; i < n; ++i)
array[i] = i;
for (int i = n - 1; i > 0; --i) {
int j = rand() % i;
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
for (int i = 0; i < n; ++i)
printf("[%d] = %d\n", i, array[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment