C Program to Implement Heap’s Algorithm for Generating Permutations

This C program implements Heap’s Algorithm for Permutation of N numbers.

Heap’s algorithm is an algorithm used for generating all possible permutations of some given length. It was first proposed by B. R. Heap in 1963. It generates each permutation from the previous one by choosing a pair of elements to interchange.

Here is the source code of the C program to implement recursive version of Heap’s 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 <stdlib.h>
  3. int len;
  4. void swap (int *x, char *y)
  5. {
  6.     int temp;
  7.     temp = *x;
  8.     *x = *y;
  9.     *y = temp;
  10. }
  11. void print(const int *v)
  12. {
  13.     int i;
  14.     int size = len;
  15.     if (v != 0) {
  16.     for ( i = 0; i < size; i++) {
  17.         printf("%4d", v[i] );
  18.     }
  19.     printf("\n");
  20.   }
  21. }
  22. void heappermute(int v[], int n) {
  23.     int i;
  24.     if (n == 1) {
  25.         print(v);
  26. 	}
  27.     else {
  28.         for (i = 0; i < n; i++) {
  29.             heappermute(v, n-1);
  30.             if (n % 2 == 1) {
  31.                 swap(&v[0], &v[n-1]);
  32. 	    }
  33.             else {
  34.                 swap(&v[i], &v[n-1]);
  35.             }
  36. 	}
  37.     }
  38. }
  39.  
  40. int main()
  41. {
  42.    int num[11];
  43.    int  i;
  44.    printf("How many numbers you want to enter: ", len);
  45.    scanf("%d", &len);
  46.    printf("\nEnter %d numbers: ");
  47.    for ( i = 0 ; i < len; i++)
  48.        scanf("%d", &num[i]);
  49.    heappermute(num, len);
  50.    return 0;
  51. }

$ gcc heappermute.c -o heappermute
$ ./heappermute
 
How many numbers you want to enter: 4
Enter 4 numbers: 3 1 2 4
 
   3   1   2   4
   1   3   2   4
   2   3   1   4
   3   2   1   4
   1   2   3   4
   2   1   3   4
   4   1   2   3
   1   4   2   3
   2   4   1   3
   4   2   1   3
   1   2   4   3
   2   1   4   3
   4   3   2   1
   3   4   2   1
   2   4   3   1
   4   2   3   1
   3   2   4   1
   2   3   4   1
   4   3   1   2
   3   4   1   2
   1   4   3   2
   4   1   3   2
   3   1   4   2
   1   3   4   2

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.