C Program to Implement Pancake Sort

This C Program Implements Pancake Sort on Array of Integers.Pancake sorting is a variation of the sorting problem in which the only allowed operation is to reverse the elements of some prefix of the sequence.

Here is source code of the C Program to Implement Pancake Sort on Array of Integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Implement Pancake Sort on Array of Integers
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void do_flip(int *, int, int);
  8.  
  9. /*Function to implement the pancake sort*/
  10. int pancake_sort(int *list, unsigned int length)
  11. {
  12.     if (length < 2)
  13.         return 0;
  14.     int i, a, max_num_pos, moves;
  15.  
  16.     moves = 0;
  17.     for (i = length;i > 1;i--)
  18.     {
  19.         max_num_pos = 0;
  20.         for (a = 0;a < i;a++)
  21.         {
  22.             if (list[a] > list[max_num_pos])
  23.                 max_num_pos = a;
  24.         }
  25.         if (max_num_pos ==  i - 1)
  26.             continue;
  27.         if (max_num_pos)
  28.         {
  29.             moves++;
  30.             do_flip(list, length, max_num_pos + 1);
  31.         }
  32.         do_flip(list, length, i);
  33.     }
  34.     return moves;
  35. }
  36.  
  37. /*Function to do flips in the elements*/
  38. void do_flip(int *list,  int length,  int num)
  39. {
  40.     int swap;
  41.     int i = 0;
  42.     for (i;i < --num;i++)
  43.     {
  44.         swap = list[i];
  45.         list[i] = list[num];
  46.         list[num] = swap;
  47.     }
  48. }
  49.  
  50. /*Function to print the array*/
  51. void print_array(int list[], int length)
  52. {
  53.     int i;
  54.     for (i = 0;i < length;i++)
  55.     {
  56.         printf("%d ", list[i]);
  57.     }
  58. }
  59.  
  60. int main(int argc,  char **argv)
  61. {
  62.    int list[9];
  63.    int i;
  64.    printf("enter the 9 elements of array:\n");
  65.    for (i = 0;i < 9;i++)
  66.        scanf("%d", &list[i]);
  67.    printf("\nOriginal: ");
  68.    print_array(list, 9);
  69.    int moves  =  pancake_sort(list, 9);
  70.    printf("\nSorted: ");
  71.    print_array(list, 9);
  72.    printf(" - with a total of %d moves\n",  moves);
  73. }

$ cc pancake.c
$ a.out
enter the 9 elements of array:
10
9
8
7
6
5
4
3
2
 
Original: 10 9 8 7 6 5 4 3 2
Sorted: 2 3 4 5 6 7 8 9 10   - with a total of 0 moves
$ a.out
enter the 9 elements of array:
1
2
3
4
5
6
7
8
9
 
Original: 1 2 3 4 5 6 7 8 9
Sorted: 1 2 3 4 5 6 7 8 9   - with a total of 0 moves
$ a.out
enter the 9 elements of array:
5
6
7
8
9
1
4
2
3
Original: 5 6 7 8 9 1 4 2 3
Sorted: 1 2 3 4 5 6 7 8 9   - with a total of 3 moves

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 wish to look at programming examples on all topics, go to C Programming Examples.

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.