Java Program for Pancake Sorting



In this article, we will learn pancake sorting which is a unique way to sort an array by flipping sections of it. The algorithm finds the largest unsorted element, flipping it to the front, and then flipping it to its correct position. Although not the fastest sorting method, it's an interesting and creative way to learn about problem-solving and data manipulation. This article explains the algorithm in simple steps and shows how to implement it in Java.

What is Pancake Sorting?

Pancake sorting is a sorting technique that resembles selection sort, i.e. sorting the largest element first thereby reducing the size of the array and eventually sorting all the elements. In pancake sorting, the idea is to sort the array elements by making the least number of reversals. 

Implement Pancake Sorting

Following are the steps for Pancake sorting ?

  • Find the index of the maximum element in the unsorted portion of the array.
  • Flip the array from the start to the maximum element to bring it to the front.
  • Flip the entire unsorted portion to move the maximum element to its correct position.
  • Repeat for the remaining unsorted portion of the array.

We will be using the java.io package for input and output functions. Function to perform pancake sort ?

public static void pancakeSort(int[] arr) {}
Loop through the array using for loop to place elements in their correct positions ?
for (int currSize = n; currSize > 1; currSize--) {}
Find the index of the largest element in the unsorted Array ?
int maxIndex = findMaxIndex(arr, currSize);
 Flip the largest element to the front ?
flip(arr, maxIndex + 1);
Flip it to its correct position ?
flip(arr, currSize);

Example

Following is an example of Pancake sorting in Java ?

import java.io.*;
public class pancake_sorting {
   static void flip_array(int my_arr[], int i) {
      int temp, beg = 0;
      while (beg < i) {
         temp = my_arr[beg];
         my_arr[beg] = my_arr[i];
         my_arr[i] = temp;
         beg++;
         i--;
      }
   }
   static int find_index(int my_arr[], int n) {
      int max_ele, i;
      for (max_ele = 0, i = 0; i < n; ++i)
         if (my_arr[i] > my_arr[max_ele])
         max_ele = i;
      return max_ele;
   }
   static int pancake_sort(int my_arr[], int n) {
      for (int curr_size = n; curr_size > 1; --curr_size) {
         int max_ele = find_index(my_arr, curr_size);
         if (max_ele != curr_size - 1) {
            flip_array(my_arr, max_ele);
            flip_array(my_arr, curr_size - 1);
         }
      }
      return 0;
   }
   public static void main(String[] args) {
      int my_arr[] = { 67, 43, 89, 11, 23, 0, 98, 102, 4 };
      int arr_len = my_arr.length;
      pancake_sort(my_arr, arr_len);
      System.out.println("The sorted array is : ");
      for (int i = 0; i < arr_len; i++)
      System.out.print(my_arr[i] + " ");
      System.out.println("");
   }
}

Output

The sorted array is :
0 4 11 23 43 67 89 98 102

Time Complexity:O(n2)
Space Complexity: O(1)

Conclusion

Pancake sorting is a unique algorithm that showcases the power of flipping operations in sorting. With the Java implementation provided, you can experiment with different inputs and understand how this technique works in detail.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-13T13:54:50+05:30

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements