C Program to Implement Fisher-Yates Algorithm

This C program implements Fisher-Yates algorithm for array shuffling. The Fisher–Yates shuffle (named after Ronald Fisher and Frank Yates), also known as the Knuth shuffle (after Donald Knuth), is an algorithm for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set. A variant of the Fisher–Yates shuffle, known as Sattolo’s algorithm, may be used to generate random cycles of length n instead. The Fisher–Yates shuffle is unbiased, so that every permutation is equally likely. The modern version of the algorithm is also rather efficient, requiring only time proportional to the number of items being shuffled and no additional storage space.

Here is the source code of the C program to shuffle an array using Fisher-Yates algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. static int rand_int(int n) {
  7.     int limit = RAND_MAX - RAND_MAX % n;
  8.     int rnd;
  9.  
  10.     do {
  11.         rnd = rand();
  12.     } 
  13.     while (rnd >= limit);
  14.     return rnd % n;
  15. }
  16.  
  17. void shuffle(int *array, int n) {
  18.     int i, j, tmp;
  19.  
  20.     for (i = n - 1; i > 0; i--) {
  21.         j = rand_int(i + 1);
  22.         tmp = array[j];
  23.         array[j] = array[i];
  24.         array[i] = tmp;
  25.    }
  26. }
  27. int main(void)
  28. {
  29.  
  30.     int i = 0;
  31.     int numbers[50];
  32.     for (i = 0; i < 50; i++)
  33.         numbers[i]= i;
  34.     shuffle(numbers, 50);
  35.     printf("\nArray after shuffling is: \n");
  36.     for ( i = 0; i < 50; i++)
  37.         printf("%d\n", numbers[i]);
  38.     return 0;
  39. }

$ gcc fisher_yates.c -o fisher_yates
$ ./fisher_yates
 
Array after shuffling is:
26
41
32
18
45
48
8
35
44
31
10
30
24
1
12
13
40
0
43
47
27
42
4
14
49
36
6
19
5
11
7
37
34
28
21
46
38
20
16
2
17
15
3
22
25
29
23
9
39
33

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.