Professional Documents
Culture Documents
Pancake Sort
Pancake Sort
SORT
Tsering Dikey Lama
NP03A180180
Agenda
Introduction
Problem Definition
History
Pseudo Code
Complexity Analysis
Application
Limitation
Conclusion
Introduction
A colloquial term for the problem of sorting a
disordered stack of pancakes in order of size.
Only one operation : flip(0, i);
An open math problem
Problem Definition
It sorts an unsorted list of array.
History
Introduced by Jacob E. Goodman
1975 A.D.
Bill gates also wrote a paper called “Bounds for
sorting by Prefix Reversal” on the upper bound of
Pancake sort in 1979.
Maximum flips = (5n+5)/3
Team of researchers from UT-Dallas, 18n/11
PseudoCode
static int pancakeSort(int array[]) {
int n = array.length;
for (int unsortASize=n; unsortASize>1;--unsortASize) {
int idxMax = findMax(array,unsortASize);
if (idxMax != unsortASize-1) {
flip(array, idxMax);
flip(array, unsortASize-1);
}
}
return 0;
}
PseudoCode
static int findMax(int array[], int n) {
int idxMax = 0;
for (int i = 0; i < n; i++) {
if (array[i] > array[idxMax]) {
idxMax = i;
}
}
return idxMax;
}
}
PseudoCode
static void flip(int array[], int idx) {
int temp, start = 0;
while (start < idx) {
temp = array[start];
array[start] = array[idx];
array[idx] = temp;
start++;
idx--;
}
Complexity Analysis
Best-Case = O(n)
Worst Case = O(n^2)
Applications
Prefix Reversal
Element Uniqueness
To find the largest and smallest element
Limitations
Things get really large really fast.
High time complexity
Only one operation
Conclusion
A sorting algorithm based on flip mechanism
An open Math problem
Best case = O(n)
Worst case = O(n^2)