Question: Hi how to do this in python Exercise 1 -Pancake Sorting Pancake Sort is a colloquial term for the mathematical problem of sorting a disordered

 Hi how to do this in python Exercise 1 -Pancake Sorting

Hi how to do this in python

Exercise 1 -Pancake Sorting Pancake Sort is a colloquial term for the mathematical problem of sorting a disordered stack of pancakes in order of size. A spatula can be inserted at any point i in the stack and used to flip all pancakes above it. Given an unsorted array, design and implement the pancake algorithm to sort it using only the "flip" operation, whose effect is to reverse the elements of the array between elements 0 and i (with i being the position where you imagine having inserted the spatula for flipping). Whereas a traditional sorting algorithm attempts to sort with the fewest comparisons possible, the goal here is to sort the sequence in as few "flips" as possible. Your algorithm should return the indices at which flips were performed. Example: Unsorted Input: [3,2,4,1] Sorted Output: [1,2,3,4] Algorithm Output: [1,2,3] indices where flips where performed (3 flips total). We have performed 3 flips (bold indicates it has been flipped): Start: [3,2,4,1] 1st flip (k=1):[2,3,4,1] 2nd flip (k=2):[4,3,2,1] 3rd flip (k=3):[1,2,3,4] Hint Intuitively, this problem can be solved by: - Finding the largest out-of-order value - Flip that largest unsorted value to the bottom (you may need to flip it to the top first) - Repeat until the pancake stack is ordered

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!