hw_3_soln - CS 473 Algorithms Fall 2010 HW 3 Solutions 1(25...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 it’s 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 it’s 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 - PosLar ge + 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
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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. Base case : When k = 0, it is true that the first 0 elements of A are sorted. Inductive Step : At the end of the k + 1 iteration, the ( k + 1 ) largest element is placed in the ( k + 1 ) th spot, its position in the correctly sorted array. We know from the induction hypothesis that the elements from 1,..., k are correctly sorted. Therefore, we now have that the 1,..., k + 1 elements are correctly sorted (the first k +
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern