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: The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Tutorial 1 (Solution) Below are the solutions of the problems of the tutorial. Question 1 : Follow the example of Lecture 2 and illustrate the MCS algo- rithm with the following input: 4-3 5-2-1 2 6-2 Solution: 1 Question 2 : Suppose you are given an increasing sequence of n distinct in- tegers in an array A [1 ..n ]. Design an efficient algorithm to determine whether there exists an index i such that A [ i ] = i . Justify the correctness of your algorithm and analyze its running time. Solution: Idea of the algorithm The algorithm starts from the middle of the array A and check if we have found a match (i.e. A [ mid ] = mid ). If not, we check to see if what we’ve got is greater or lesser than the required value. If it is greater (i.e. A [ mid ] > mid ), then we can confidently ignore all the values greater than and including the mid point. If it is smaller (i.e. A [ mid ] < mid ), then we...
View Full Document
This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.
- Spring '07