Advertisement
cd62131

Shuffle Array

Dec 16th, 2013
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void shuffle(int *a, int size) {
  4.   int i, j, b;
  5.   for (i = size - 1; i > 1; i--) {
  6.     j = rand() % (i - 1);
  7.     b = a[i];
  8.     a[i] = a[j];
  9.     a[j] = b;
  10.   }
  11. }
  12.  
  13. int main(void) {
  14.   int i, j, size;
  15.   int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  16.   size = sizeof(a) / sizeof(int);
  17.   for (i = 0; i < 20; i++) {
  18.     shuffle(a, size);
  19.     for (j = 0; j < size; j++) {
  20.       printf("%d", a[j]);
  21.       if (j < size - 1) printf(" ");
  22.     }
  23.     printf("\n");
  24.   }
  25.   return EXIT_SUCCESS;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement