Advertisement
Guest User

fisher yates shuffle

a guest
Jul 30th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <random>
  4. #include <conio.h>
  5.  
  6. void main()
  7. {
  8.     int myArray[] = { 1,2,3,4,5,20,40,21,10,94,44 };
  9.     int arrayLength = sizeof(myArray) / sizeof(int);
  10.     for (size_t i = 0; i < arrayLength - 1; i++)
  11.     {
  12.         int index = rand() % (i + 1);
  13.         int arrayTemp = myArray[index];
  14.         myArray[index] = myArray[i];
  15.         myArray[i] = arrayTemp;
  16.     }
  17.     for (int i = 0; i < arrayLength; i++) {
  18.         printf("%d ", myArray[i]);
  19.         //printf("\n");
  20.     }
  21.     _getch();
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement