This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 473: Algorithms, Fall 2010 HW 3 Solutions 1. (25 pts) Suppose we have a stack of n pancakes of different sizes. We want to sort the pancakes so that the smaller pancakes are on top of the larger pancakes. The only operation we can perform is a flip insert a spatula under the top k pancakes, for some k between 1 and n , and flip them all over. Name: CS 373 Midterm 1 4. Suppose we have a stack of n pancakes of different sizes. We want to sort the pancakes so that smaller pancakes are on top of larger pancakes. The only operation we can perform is a flip insert a spatula under the top k pancakes, for some k between 1 and n , and flip them all over. Flipping the top three pancakes (a) [3 pt] Describe an algorithm to sort an arbitrary stack of n pancakes. (b) [3 pt] Prove that your algorithm is correct. (c) [2 pt] Exactly how many flips does your algorithm perform in the worst case? (A correct () bound is worth one point.) (d) [2 pt] Suppose one side of each pancake is burned. Exactly how many flips do you need to sort the pancakes and have the burned side of every pancake on the bottom? (A correct () bound is worth one point.) 4 (15 pts) Describe an algorithm to sort an arbitrary stack of n pancakes and give a bound on the number of flips that the algorithm makes. Assume that the pancake information is given to you in the form of an n element array A . A [ i ] is a number between 1 and n and A [ i ] = j means that the j th smallest pancake is in position i from the bottom; in other words A [ 1 ] is the size of the bottom most pancake (relative to the others) and A [ n ] is the size of the top pancake. Assume you have the operation Flip ( k ) which will flip the top k pancakes. Note that you are only interested in minimizing the number of flips. Solution: Main Idea : Our algorithm will start by finding the largest pancake. If its not in the correct position, then we flip it to the top of the stack and flip it one more time to the very bottom. The algorithm will proceed by finding the next largest pancake, flipping it to the top (if its not already in its correct position), and then flipping it to its correct position above the largest pancake. We do this process for each pancake. Algorithm : P ANCAKE S ORT ( A ) : n length of A For i = 1 to n If (A [ i ] 6 = ( n + 1 i ) ) PosLarge F IND I NDEX O F L ARGEST (A, i) F LIP ( n PosLarge + 1) F LIP ( n i + 1) Return A F IND I NDEX O F L ARGEST ( A , j ) : value  n length of A For i = j to n If (A [ i ] > value) value A [ i ] pos i Return pos 1 Proof of Correctness : We will prove the algorithm by induction on the number of iterations. Induction Hypothesis : At the end of the k th iteration, the first k elements of A are correctly sorted....
View Full
Document
 Spring '08
 Chekuri,C
 Algorithms, Sort

Click to edit the document details