C Program for Pancake sorting?

Pancake sorting is a variation of the sorting problem where the only allowed operation is to reverse elements of some prefix of the sequence. It's named after the problem of sorting a stack of pancakes using a spatula that can flip all pancakes above any insertion point.

Syntax

void pancakeSort(int arr[], int n);
void flip(int arr[], int i);

How Pancake Sort Works

The algorithm works by repeatedly finding the maximum element in the unsorted portion and moving it to its correct position using at most two flip operations −

  • Step 1: Find the maximum element in the current unsorted array
  • Step 2: If it's not at the top, flip it to bring it to the top
  • Step 3: Flip the entire unsorted portion to move the maximum to its correct position
  • Step 4: Reduce the size of unsorted portion and repeat

Example

Here's a complete implementation of pancake sort in C −

#include <stdio.h>

/* Function to flip arr[0..i] */
void flip(int arr[], int i) {
    int start = 0;
    while (start < i) {
        int temp = arr[start];
        arr[start] = arr[i];
        arr[i] = temp;
        start++;
        i--;
    }
}

/* Function to find index of maximum element in arr[0..n-1] */
int findMax(int arr[], int n) {
    int maxIndex = 0;
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[maxIndex])
            maxIndex = i;
    }
    return maxIndex;
}

/* Function to perform pancake sort */
void pancakeSort(int arr[], int n) {
    /* Start from the complete array and reduce current size by one */
    for (int currSize = n; currSize > 1; currSize--) {
        /* Find index of maximum element in arr[0..currSize-1] */
        int maxIndex = findMax(arr, currSize);
        
        /* Move the maximum element to end if it's not already there */
        if (maxIndex != currSize - 1) {
            /* First move maximum to beginning if it's not already */
            if (maxIndex != 0) {
                flip(arr, maxIndex);
            }
            /* Now move the maximum from beginning to end */
            flip(arr, currSize - 1);
        }
    }
}

/* Function to print array */
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
}

int main() {
    int arr[] = {5, 3, 2, 1, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("Original array: ");
    printArray(arr, n);
    
    pancakeSort(arr, n);
    
    printf("Sorted array: ");
    printArray(arr, n);
    
    return 0;
}
Original array: 5 3 2 1 4 
Sorted array: 1 2 3 4 5 

Key Points

  • Time Complexity: O(n²) where n is the number of elements
  • Space Complexity: O(1) as it sorts in-place
  • Number of Flips: At most 2(n-1) flips are needed to sort n elements
  • Unlike comparison-based sorting, pancake sort minimizes the number of prefix reversals

Conclusion

Pancake sorting is an interesting algorithmic problem that demonstrates how constraints can lead to unique sorting approaches. While not practically efficient, it provides valuable insights into algorithm design and optimization under specific operational limitations.

Updated on: 2026-03-15T11:42:02+05:30

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements