Preview

Sorting Algorithm

Better Essays
Open Document
Open Document
12285 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Sorting Algorithm
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 2n 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(n2 ) 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

8.

You May Also Find These Documents Helpful

Related Topics