## Educators

Problem 1

List all the steps used by Algorithm 1 to find the maximum of the list $1,8,12,9,11,2,14,5,10,4$

Bryan L.

Problem 2

Determine which characteristics of an algorithm described in the text (after Algorithm 1 ) the following procedures have and which they lack.
a) procedure double $(n : \text { positive integer })$ while $n>0$ $n :=2 n$
b) procedure divide(n: positive integer)
while $n \geq 0$
$m :=1 / n$
$n :=n-1$
c) procedure sum(n: positive integer)
sum $:=0$
while $i<10$
sum $:=\operatorname{sum}+i$
d) procedure choose $(a, b : \text { integers })$
$x :=$ either $a$ or $b$

Bryan L.

Problem 3

Devise an algorithm that finds the sum of all the integers in a list.

Bryan L.

Problem 4

Describe an algorithm that takes as input a list of $n$ integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it.

Bryan L.

Problem 5

Describe an algorithm that takes as input a list of $n$ integers in nondecreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is nondecreasing if each integer in the list is at least as large as the previous integer in the list.)

Bryan L.

Problem 6

Describe an algorithm that takes as input a list of $n$ integers and finds the number of negative integers in the list.

Bryan L.

Problem 7

Describe an algorithm that takes as input a list of $n$ integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list.

Bryan L.

Problem 8

Describe an algorithm that takes as input a list of $n$ distinct integers and finds the location of the largest eveninteger in the list or returns 0 if there are no even integers in the list.

Bryan L.

Problem 9

A palindrome is a string that reads the same forward and backward. Describe an algorithm for determining whether a string of $n$ characters is a palindrome.

Bryan L.

Problem 10

Devise an algorithm to compute $x^{n}$ , where $x$ is a real number and $n$ is an integer. [Hint: First give a procedure for computing $x^{n}$ when $n$ is nonnegative by successive multiplication by $x,$ starting with $1 .$ Then extend this procedure, and use the fact that $x^{-n}=1 / x^{n}$ to compute $x^{n}$ when
$n$ is negative. $]$

Bryan L.

Problem 11

Describe an algorithm that interchanges the values of the variables $x$ and $y,$ using only assignments. What is the minimum number of assignment statements. What is the this?

Bryan L.

Problem 12

Describe an algorithm that uses only assignment state- ments that replaces the triple $(x, y, z)$ with $(y, z, x) .$ What is the minimum number of assignment statements needed?

Bryan L.

Problem 13

List all the steps used to search for 9 in the sequence 1 $3,4,5,6,8,9,11$ using
a) a linear search. $\quad$ b) a binary search.

Bryan L.

Problem 14

List all the steps used to search for 7 in the sequence given in Exercise 13 for both a linear search and a binary search.

Bryan L.

Problem 15

Describe an algorithm that inserts an integer $x$ in the appropriate position into the list $a_{1}, a_{2}, \ldots, a_{n}$ of integers that are in increasing order.

Bryan L.

Problem 16

Describe an algorithm for finding the smallest integer in a finite sequence of natural numbers.

Bryan L.

Problem 17

Describe an algorithm that locates the first occurrence of the largest element in a finite list of integers, where the integers in the list are not necessarily distinct.

Bryan L.

Problem 18

Describe an algorithm that locates the last occurrence of the smallest element in a finite list of integers, where the integers in the list are not necessarily distinct.

Bryan L.

Problem 19

Describe an algorithm that produces the maximum, median, mean, and minimum of a set of three integers. (The median of a set of integers is the middle element in the list when these integers are listed in order of increasing size. The mean of a set of integers is the sum of the integers divided by the number of integers in the set.)

Bryan L.

Problem 20

Describe an algorithm for finding both the largest and the smallest integers in a finite sequence of integers.

Bryan L.

Problem 21

Describe an algorithm that puts the first three terms of a sequence of integers of arbitrary length in increasing order.

Bryan L.

Problem 22

Describe an algorithm to find the longest word in an English sentence (where a sentence is a sequence of symbols, either a letter or a blank, which can then be broken into alternating words and blanks).

Bryan L.

Problem 23

Describe an algorithm that determines whether a function from a finite set of integers to another finite set of integers is onto.

Bryan L.

Problem 24

Describe an algorithm that determines whether a function from a finite set to another finite set is one-to-one.

Bryan L.

Problem 25

Describe an algorithm that will count the number of 1 $\mathrm{s}$in a bit string by examining each bit of the string to determine whether it is a 1 bit.

Bryan L.

Problem 26

Change Algorithm 3 so that the binary search procedure compares $x$ to $a_{m}$ at each stage of the algorithm, with the algorithm terminating if $x=a_{m} .$ What advantage does this version of the algorithm have?

Bryan L.

Problem 27

The ternary search algorithm locates an element in a list of increasing integers by successively splitting the list into three sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. Specify the steps of this algorithm.

Bryan L.

Problem 28

Specify the steps of an algorithm that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece.

Bryan L.

Problem 29

Devise an algorithm that finds a mode in a list of nonde- creasing integers. (Recall that a list of integers is nondecreasing if each term is at least as large as the preceding term.)

Bryan L.

Problem 30

Devise an algorithm that finds all modes. (Recall that list of integers is nondecreasing if each term of the list is at least as large as the preceding term.)

Bryan L.

Problem 31

Two strings are anagrams if each can be formed from the other string by rearranging its characters. Devise an algorithm to determine whether two strings are anagrams
a) by first finding the frequency of each character that appears in the strings.
b) by first sorting the characters in both strings.

Check back soon!

Problem 32

Given $n$ real numbers $x_{1}, x_{2}, \ldots, x_{n},$ find the two that are closest together by
a) a brute force algorithm that finds the distance between every pair of these numbers.
b) sorting the numbers and computing the least number of distances needed to solve the problem.

Bryan L.

Problem 33

Devise an algorithm that finds the first term of a sequence of integers that equals some previous term in the sequence.

Bryan L.

Problem 34

Devise an algorithm that finds all terms of a finite sequence of integers that are greater than the sum of all
previous terms of the sequence.

Bryan L.

Problem 35

Devise an algorithm that finds the first term of a sequence of positive integers that is less than the immediately preceding term of the sequence.

Bryan L.

Problem 36

Use the bubble sort to sort $6,2,3,1,5,4,$ showing the lists obtained at each step.

Bryan L.

Problem 37

Use the bubble sort to sort $3,1,5,7,4,$ showing the lists obtained at each step.

Bryan L.

Problem 38

Use the bubble sort to sort $d, f, k, m, a, b,$ showing the lists obtained at each step.

Bryan L.

Problem 39

Adapt the bubble sort algorithm so that it stops when no interchanges are required. Express this more efficient version of the algorithm in pseudocode.

Bryan L.

Problem 40

Use the insertion sort to sort the list in Exercise $36,$ showing the lists obtained at each step.

Bryan L.

Problem 41

Use the insertion sort to sort the list in Exercise $37,$ showing the lists obtained at each step.

Bryan L.

Problem 42

Use the insertion sort to sort the list in Exercise $38,$ showing the lists obtained at each step.

Bryan L.

Problem 43

Sort these lists using the selection sort.
$\begin{array}{ll}{\text { a) } 3,5,4,1,2} & {\text { b) } 4,3,2,1} \\ {\text { c) } 1,2,3,4,5}\end{array}$

Bryan L.

Problem 44

Write the selection sort algorithm in pseudocode.

Bryan L.

Problem 45

Describe an algorithm based on the linear search for determining the correct position in which to insert a new element in an already sorted list.

Bryan L.

Problem 46

Describe an algorithm based on the binary search for determining the correct position in which to insert a new element in an already sorted list.

Bryan L.

Problem 47

How many comparisons does the insertion sort use to sort the list $1,2, \ldots, n ?$

Bryan L.

Problem 48

How many comparisons does the insertion sort use to sort the list $n, n-1, \ldots, 2,1 ?$

Bryan L.

Problem 49

Show all the steps used by the binary insertion sort to sort the list $3,2,4,5,1,6$

Bryan L.

Problem 50

Compare the number of comparisons used by the insertion sort and the binary insertion sort to sort the list $7,4,$ $3,8,1,5,4,2 .$

Bryan L.

Problem 51

Express the binary insertion sort in pseudocode.

Check back soon!

Problem 52

a) Devise a variation of the insertion sort that uses a linear search technique that inserts the $j$ th element in the correct place by first comparing it with the $(j-1)$ st element, then the $(j-2)$ th element if necessary, and so on.
b) Use your algorithm to sort $3,2,4,5,1,6$
c) Answer Exercise 47 using this algorithm.
d) Answer Exercise 48 using this algorithm.

Check back soon!

Problem 53

When a list of elements is in close to the correct order, would it be better to use an insertion sort or its variation described in Exercise 52$?$

Bryan L.

Problem 54

List all the steps the naive string matcher uses to find all occurrences of the pattern $\mathrm{FE}$ in the text COVFEFE.

Bryan L.

Problem 55

List all the steps the naive string matcher uses to find all occurrences of the pattern $\mathrm{ACG}$ in the text TACAGACG.

Bryan L.

Problem 56

Use the cashier's algorithm to make change using quarters, dimes, nickels, and pennies for
$\begin{array}{ll}{\text { a) } 87 \text { cents. }} & {\text { b) } 49 \text { cents. }} \\ {\text { c) } 99 \text { cents. }} & {\text { d) } 33 \text { cents. }}\end{array}$

Bryan L.

Problem 57

Use the cashier's algorithm to make change using quarters, dimes, nickels, and pennies for
$\begin{array}{ll}{\text { a) } 51 \text { cents. }} & {\text { b) } 69 \text { cents. }} \\ {\text { c) } 76 \text { cents. }} & {\text { d) } 60 \text { cents. }}\end{array}$

Bryan L.

Problem 58

Use the cashier's algorithm to make change using quarters, dimes, and pennies (but no nickels) for each of the amounts given in Exercise $56 .$ For which of these amounts does the greedy algorithm use the fewest coins of these denominations possible?

Bryan L.

Problem 59

Use the cashier's algorithm to make change using quarters, dimes, and pennies (but no nickels) for each of the amounts given in Exercise 57 . For which of these amounts does the greedy algorithm use the fewest coins of these denominations possible?

Check back soon!

Problem 60

Show that if there were a coin worth 12 cents, the cashier's algorithm using quarters, 12 -cent coins, dimes, nickels, and pennies would not always produce change using the fewest coins possible.

Bryan L.

Problem 61

Use Algorithm 7 to schedule the largest number of talks in a lecture hall from a proposed set of talks, if the starting and ending times of the talks are $9 : 00$ A. M. and $9 : 45$ A.M.
$9 : 30$ A.M. and $10 : 00$ A.M.; $9 : 50$ A.M. and $10 : 15$ A.M.:
$10 : 00$ A.M. and $10 : 30$ A.M.; $10 : 10$ A.M. and $10 : 25$ A.M.;
$10 : 30$ A.M. and $10 : 55$ A.M.; $10 : 15$ A.M. and $10 : 45$ A.M.:
$10 : 30$ A.M. and $11 : 00$ A.M.; $10 : 45$ A.M. and $11 : 30$ A.M.;
$10 : 55$ A.M. and $11 : 25 \mathrm{A} . \mathrm{M} . ; 11 : 00$ A.M. and $11 : 15 \mathrm{A.M.}$

Bryan L.

Problem 62

Show that a greedy algorithm that schedules talks in a lecture hall, as described in Example $7,$ by selecting at each step the talk that overlaps the fewest other talks, does not always produce an optimal schedule.

Check back soon!

Problem 63

a) Devise a greedy algorithm that determines the fewest lecture halls needed to accommodate $n$ talks given the starting and ending time for each talk.
b) Prove that your algorithm is optimal.
Suppose we have $s$ men $m_{1}, m_{2}, \ldots, m_{s}$ and $s$ women $w_{1}, w_{2}, \ldots, w_{s} .$ We wish to match each person with a member of the opposite gender. Furthermore, suppose that each person ranks, in order of preference, with no ties, the people of the opposite gender. We say that a matching of people of opposite genders to form couples is stable if we cannot find a man $m$ and a woman $w$ who are not assigned to each other such that $m$ prefers $w$ over his assigned partner and $w$ prefers $m$ to her assigned partner.

Check back soon!

Problem 64

Suppose we have three men $m_{1}, m_{2},$ and $m_{3}$ and three women $w_{1}, w_{2},$ and $w_{3} .$ Furthermore, suppose that the preference rankings of the men for the three women, from highest to lowest, are $m_{1} : w_{3}, w_{1}, w_{2} ; m_{2} : w_{1}, w_{2}, w_{3} ; m_{3}$ $w_{2}, w_{3}, w_{1} ;$ and the preference rankings of the women for the three men, from highest to lowest, are $w_{1} : m_{1}, m_{2}, m_{3}$ $w_{2} : m_{2}, m_{1}, m_{3} ; w_{3} : m_{3}, m_{2}, m_{1} .$ For each of the six possible matchings of men and women to form three couples, determine whether this matching is stable.

Bryan L.

Problem 65

Write the deferred acceptance algorithm in pseudocode.

Check back soon!

Problem 66

Show that the deferred acceptance algorithm terminates.

Bryan L.

Problem 67

Show that the deferred acceptance always terminates with a stable assignment.

Bryan L.

Problem 68

a) Explain why a sequence has at most one majority element.
b) Show all the steps of the Boyer-Moore majority vote algorithm when given the sequence $2,1,3,3,2,3 .$
c) Express the Boyer-Moore majority vote algorithm in
pseudocode.
d) Explain how you can determine whether the majority candidate element produced by the Boyer-Moore
algorithm is actually a majority element.

Check back soon!

Problem 69

a) Prove that the Boyer-Moore majority vote algorithm outputs the majority element of a sequence, if it exists. Brove or disprove that the majority candidate of the Boyer-Moore majority vote algorithm will be a mode of the sequence (that is, its most common element) even when no majority element exists.

Check back soon!

Problem 70

Show that the problem of determining whether a program with a given input ever prints the digit 1 is unsolvable.

Bryan L.

Problem 71

Show that the following problem is solvable. Given two programs with their inputs and the knowledge that exactly one of them halts, determine which halts.

Bryan L.

Problem 72

Show that the problem of deciding whether a specific program with a specific input halts is solvable.

Bryan L.