# hw2 - CS161 Homework 2 Due Thursday July 9 5pm 1(24 points...

This preview shows pages 1–2. Sign up to view the full content.

CS161 - Homework 2 Due: Thursday July 9, 5pm 1. (24 points, 3 each) (CLRS 4-4, subproblems a-g,j (pg. 86). You only need to provide upper bounds (i.e. O ) for recurrences you don’t solve using the master method. 2. (5 points) CLRS 4.3-2 (pg. 75). Explain your answer. 3. (25 points) There are some situations where we are asked to sort data that is almost sorted. A k -sorted array contains no element that is more than k positions from it’s position in the properly sorted array. For the questions below, A is a k -sorted array with k << n : (a) (10 points) What are the runtimes of Insertion-Sort , Merge-Sort and Quicksort on A ? Explain your answers. (b) (5 points) What is the runtime of Bubble-Sort on A (see pseu- docode below)? Explain your answer. Bubble-Sort(A) 1 sorted false 2 while sorted = false 3 sorted true 4 for i 1 to length [ A ] - 1 5 if A [ i ] > A [ i + 1] 6 swap A [ i ] and A [ i + 1] 7 sorted false (c) (10 points) Write pseudocode and provide the runtime for an algorithm that performs better than the above algorithms for sorting a k -sorted array.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern