{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
This file 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 difficult 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 and leave the boys in joint possession of the boat? How many times need the boat pass from shore to shore? 2. Alternating glasses There are 2 n glasses standing next to each other in a row, the first n of them filled with a soda drink while the remaining n glasses are empty. Make the glasses alternate in a filled-empty-filled-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 first 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 efficiency 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 ) efficiency 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 efficiency? How is it compared to that of the version given in the text? 1
Image of page 1

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

View Full Document Right Arrow Icon
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 find an appropriate position to insert A [ i ] among the previously sorted A [0] ... A [ i 1] . Determine the worst-case efficiency 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 predefined decreasing sequence of step sizes, h 1 > ... > h i > ... > 1 , which must end with 1 .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern