Created
August 21, 2019 13:29
-
-
Save guidanoli/5b0b428e585fd46f432c8311b4b012c8 to your computer and use it in GitHub Desktop.
Array permutation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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