h3_sol

# h3_sol - CSE 202 Homework 3 Solution TA: Nan Zang The...

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

CSE 202 Homework 3 Solution TA: Nan Zang The solutions in this homework are not complete. I only gave some directions to solve the problem. For algorithm problem, you must give the algorithm, analysis of correctness and time complexity. Problem 1 Exercise 5 - 2 Very similar to the algorithm given in class, and only modify the merge procedure given in the book. Problem 2 Exercise 5 - 3 (Divide and Conquer Algorithm) Definition of Subproblem: Majority(k, S): Given k cards, if there are more than k/2 cards equivalent to one another, return true and give the card number. Base Case: If there is only one card available, return true and its number. If there are two cards available, return false if the two cards are different; return true and its number, otherwise. DQ Algorithm (bool, num) Majority (n, S) // S is the set of numbers; return false or (true, value) 1: If(n =1) return true and its number; 2: If (n=2) return false if the two cards are different; return true and its number, otherwise. 3: Else{ //For simplicity, I consider the case that n is even. 4: Split the cards into two equal sets S1 ={1, 2, …. ., n/2} S2 = {n/2+1, …, n}; (b1, value1) = Majority (n/2, S1); //b1,value1 are the return values from subproblem 1 (b2, value2) = Majority (n/2, S2); //b2,value2 are the return values from subproblem 2 5: If(b1 is false and b2 is false) Return false 6: Else if (b1 is false and b2 is true) { Compare value2 with all the cards in S to see if there are more than n/2 cards equivalent to it.

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.

## This note was uploaded on 05/21/2008 for the course CS CO453 taught by Professor Graham during the Winter '08 term at UCSD.

### Page1 / 4

h3_sol - CSE 202 Homework 3 Solution TA: Nan Zang The...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online