# solu5 - This le contains the exercises, hints, and...

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

This fle contains the exercises, hints, and solutions For Chapter 5 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 5.1 1. Ferrying soldiers A detachment oF n soldiers must cross a wide and deep river with no bridge in sight. They notice two 12-year-old boys playing in a rowboat by the shore. The boat is so tiny, however, that it can only hold two boys or one soldier. How can the soldiers get across the river andleavetheboysinjo intpossess ionoFtheboat? Howmanyt imesneed the boat pass From shore to shore? 2. ± Alternating glasses There are 2 n g lassesstand ingnexttoeachother in a row, the frst n oF them flled with a soda drink while the remaining n glasses are empty. Make the glasses alternate in a flled-empty-flled-empty pattern in the minimum number oF glass moves. [Gar78], p.7. 3. Design a decrease-by-one algorithm For generating the power set oF a set oF n elements. (The power set oF a set S is the set oF all the subsets oF S , including the empty set and S itselF.) 4. Apply insertion sort to sort the list E, X, A , M , P , L , E in alphabetical order. 5. a. What sentinel should be put beFore the frst element oF an array being sorted in order to avoid checking the in-bound condition j 0 on each iteration oF the inner loop oF insertion sort? b. Will the version with the sentinel be in the same eﬃciency class as the original version? 6. Is it possible to implement insertion sort For sorting linked lists? Will it have the same O ( n 2 ) eﬃciency as the array version? 7. Consider the Following version oF insertion sort. Algorithm InsertSort2 ( A [0 ..n 1]) for i 1 to n 1 do j i 1 while j 0 and A [ j ] >A [ j +1] do swap ( A [ j ] , A [ j +1]) j j 1 What is its time eﬃciency? How is it compared to that oF the version g iveninthetext? 1

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

View Full Document
8. Let A [0 ..n 1] be an array of n sortable elements. (For simplicity, you can assume that all the elements are distinct.) Recall that a pair of its elements ( A [ i ] ,A [ j ]) is called an inversion if i<j and A [ i ] >A [ j ] . a. What arrays of size n have the largest number of inversions and what is this number? Answer the same questions for the smallest number of inversions. b. ± Show that the average-case number of key comparisons in insertion sort is given by the formula C avg ( n ) n 2 4 . 9. ² Binary insertion sort uses binary search to ±nd an appropriate position to insert A [ i ] among the previously sorted A [0] ... A [ i 1] . Determine the worst-case eﬃciency class of this algorithm. 10. Shellsort (more accurately Shell’s sort) is an important sorting algorithm which works by applying insertion sort to each of several interleaving sub- lists of a given list. On each pass through the list, the sublists in question are formed by stepping through the list with an increment h i taken from some prede±ned decreasing sequence of step sizes, h 1 >.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/28/2010 for the course CS CSE taught by Professor Drt during the Spring '10 term at Kaplan University.

### Page1 / 46

solu5 - This le contains the exercises, hints, and...

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

View Full Document
Ask a homework question - tutors are online