{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# solu6 - This le contains the exercises hints and solutions...

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

This file contains the exercises, hints, and solutions for Chapter 6 of the book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by A. Levitin. The problems that might be challenging for at least some students are marked by ; those that might be diﬃcult for a majority of students are marked by . Exercises 6.1 1. Recall that the median of a list of n numbers is defined as its n/ 2 smallest element. (The median is larger than one half the elements and is smaller than the other half.) Design a presorting-based algorithm for finding the median and determine its eﬃciency class. 2. Consider the problem of finding the distance between the two closest num- bers in an array of n numbers. (The distance between two numbers x and y is computed as | x y | .) a. Design a presorting-based algorithm for solving this problem and de- termine its eﬃciency class. b. Compare the eﬃciency of this algorithm with that of the brute-force algorithm (see Problem 9 in Exercises 1.2). 3. Let A = { a 1 , ... , a n } and B = { b 1 , ..., b m } be two sets of numbers. Consider the problem of finding their intersection, i.e., the set C of all the numbers that are in both A and B . a. Design a brute-force algorithm for solving this problem and deter- mine its eﬃciency class. b. Design a presorting-based algorithm for solving this problem and de- termine its eﬃciency class. 4. Consider the problem of finding the smallest and largest elements in an array of n numbers. a. Design a presorting-based algorithm for solving this problem and de- termine its eﬃciency class. b. Compare the eﬃciency of the three algorithms: (i) the brute-force algorithm, (ii) this presorting-based algorithm, and (iii) the divide-and- conquer algorithm (see Problem 2 in Exercises 4.1). 5. Show that the average-case eﬃciency of one-time searching by the algo- rithm that consists of the most eﬃcient comparison-based sorting algo- rithm followed by binary search is inferior to the average-case eﬃciency of sequential search. 1

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

View Full Document
6. Estimate how many searches will be needed to justify time spend on pre- sorting an array of 10 3 elements if sorting is done by mergesort and search- ing is done by binary search. (You may assume that all searches are for elements known to be in the array.) What about an array of 10 6 elements? 7. To sort or not to sort? Design a reasonably eﬃcient algorithm for solving each of the following problems and determine its eﬃciency class. a. You are given n telephone bills and m checks sent to pay the bills ( n m ) . Assuming that telephone numbers are written on the checks, find out who failed to pay. (For simplicity, you may also assume that only one check is written for a particular bill and that it covers the bill in full.) b. You have a file of n student records indicating each student’s num- ber, name, home address, and date of birth. Find out the number of students from each of the 50 U.S. states.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}