Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PancakeSort.c #1731

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Sorting Algorithms/PANCAKE_SORT/PancakeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>

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

int findMax(int arr[], int n) {
int maxIdx = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[maxIdx])
maxIdx = i;
}
return maxIdx;
}

void pancakeSort(int arr[], int n) {
for (int curr_size = n; curr_size > 1; curr_size--) {
int maxIdx = findMax(arr, curr_size);
if (maxIdx != curr_size - 1) {
flip(arr, maxIdx);
flip(arr, curr_size - 1);
}
}
}

void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {3, 6, 2, 7, 4, 5, 1};
int n = sizeof(arr) / sizeof(arr[0]);
pancakeSort(arr, n);
printArray(arr, n);
return 0;
}
79 changes: 79 additions & 0 deletions Sorting Algorithms/PANCAKE_SORT/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
## Pancake Sort Algorithm
## Overview

Pancake Sort is a comparison sorting algorithm that sorts an array using a series of "flips." A flip is an operation that reverses the order of elements from the beginning of the array up to a specified index. The algorithm's name comes from the analogy of sorting a stack of pancakes by flipping them, as you might when trying to place the largest pancake at the bottom and work upwards to sort the stack.

Pancake Sort is notable for its unique approach, as it only allows flipping as a sorting operation. It is not widely used for practical purposes due to its relatively high time complexity compared to more efficient algorithms (such as quicksort or mergesort). However, it is an interesting and important algorithm in computer science due to its educational value and its application in problems where reversal operations are required.

## How the Algorithm Works

1. **Find the Largest Unsorted Element**: The algorithm starts by finding the largest unsorted element in the array (from `arr[0]` to `arr[curr_size - 1]`).

2. **Flip to Move Largest Element to the Beginning**: Once the largest element is identified, the algorithm "flips" the array from the start to the index of this largest element, bringing it to the front.

3. **Flip to Move Largest Element to Its Final Position**: Next, the algorithm flips the array from the start to `curr_size - 1`, moving the largest element to its correct position at the end of the unsorted portion.

4. **Repeat**: This process continues with progressively smaller portions of the array (by reducing `curr_size` each iteration) until the array is fully sorted.

## Example

Consider the array: `[3, 6, 2, 7, 4, 5, 1]`

1. **First Pass**: Largest element is `7`.
- Flip to bring `7` to the beginning: `[7, 2, 6, 3, 4, 5, 1]`
- Flip again to place `7` in the last position: `[1, 5, 4, 3, 6, 2, 7]`

2. **Second Pass**: Largest element in `[1, 5, 4, 3, 6, 2]` is `6`.
- Flip to bring `6` to the beginning: `[6, 3, 4, 5, 1, 2, 7]`
- Flip to place `6` in the next last position: `[2, 1, 5, 4, 3, 6, 7]`

3. **Continue Until Sorted**: Repeat the above steps until the array becomes `[1, 2, 3, 4, 5, 6, 7]`.

## Time Complexity

Pancake Sort has a worst-case time complexity of **O(n²)**, where `n` is the number of elements in the array. This complexity arises because, in the worst case, each of the `n` flips involves searching for the maximum element (an O(n) operation) and then performing a flip (also an O(n) operation).

So, each iteration has a complexity of O(n), and the algorithm performs `n` iterations in total, leading to an overall complexity of O(n) * O(n) = **O(n²)**.

### Time Complexity Breakdown
- Finding the maximum element in each iteration: **O(n)**
- Performing a flip in each iteration: **O(n)**
- Total complexity for `n` iterations: **O(n²)**

## Space Complexity

The space complexity of Pancake Sort is **O(1)**. The algorithm sorts the array in-place, using only a constant amount of additional memory for variables like the index and temporary values in the flip function.

## Use Cases

Pancake Sort is primarily used for educational purposes to demonstrate sorting techniques and understand array manipulation through reversals. While it is not practical for most real-world applications due to its inefficiency, it is used as a theoretical problem in algorithms, often in coding competitions.

**Potential Applications**:
- **Reversal-Based Sorting Problems**: It is useful in situations where only reversal (or flip) operations are allowed, such as in robotic arm sorting where rotation is restricted.
- **Algorithm Competitions**: Pancake Sort appears as a problem in coding contests and competitive programming due to its unique approach and complexity.

## Advantages and Disadvantages

### Advantages
- Simple to implement.
- Demonstrates an unusual sorting method using only reversal operations.

### Disadvantages
- High time complexity (O(n²)) compared to other sorting algorithms.
- Not practical for large arrays or real-world applications where efficient sorting is needed.

## Conclusion

Pancake Sort is a fascinating algorithm with a unique approach that provides insight into different ways of sorting by only allowing flips. Although it has limited practical applications due to its inefficiency, it remains an interesting study topic and a valuable tool for learning about sorting algorithms and array manipulation techniques.
## Theoretical Implications and Complexity
Pancake Sort brings up interesting questions in combinatorics and theoretical computer science. For example:

Minimum Number of Flips: For any array, the Pancake Sorting problem also asks what the minimum number of flips required to sort it is. This is known as the Pancake Number.
Bounds on Flip Count: The maximum number of flips needed to sort an array of n elements has been shown to be between 1.5n and 2n - 3. This bound was derived using various approaches in combinatorial optimization and provides a cap on how inefficient the sorting can be.
## Variations of Pancake Sort
1. Burnt Pancake Problem
In this variation, each pancake has two sides, with one side marked as “burnt.” The goal is not only to sort the pancakes by size but also to have all burnt sides facing down in the sorted stack. This variant adds complexity, as it introduces an additional constraint with orientation.
2. Prefix Reversal Sorting
A generalization of Pancake Sort, where any prefix can be reversed to sort an array. In many programming challenges, this problem arises with the goal of minimizing the number of prefix reversals rather than sorting all elements. It's similar to the Pancake Sort problem but does not require elements to be pancakes or even of different sizes.
3. Restricted Pancake Sort
In this problem, only specific portions of the stack can be flipped (e.g., only even-numbered pancakes). This variation simulates real-world applications where certain elements are not accessible for direct manipulation due to physical or logical constraints.
Loading