• Home
  • Textbooks
  • Invitation to Computer Science
  • The efficiency of algorithms

Invitation to Computer Science

G.Michael Schneider, Judith Gersting

Chapter 3

The efficiency of algorithms - all with Video Answers

Educators


Chapter Questions

01:05

Problem 1

a. Use Gauss's approach to find the following sum:
$$
2+4+6+\ldots+100
$$
b. Use Gauss's approach to find a formula for the sum of the even numbers from 2 to $2 n$ :
$$
2+4+6+\ldots+2 n
$$
Your formula will be an expression involving $n$.

Amy Jiang
Amy Jiang
Numerade Educator
01:10

Problem 2

An English Christmas carol, "The Twelve Days of Christmas," dates from the late 1700 s. The twelve verses in the song are cumulative, each verse adding an additional gift given by "my true love." The twelfth verse says "On the twelfth day of Christmas, my true love gave to me ..."
12 Drummers Drumming
10 Lords-a-Leaping
... and so forth down to ...
3 French Hens
2 Turtle Doves
And a Partridge in a Pear Tree.
a. Use Gauss's formula to find the total number of gifts given on Day 12.
b. How many total gifts are given over all 12 days? Hint:
$$
1(2)+2(3)+3(4)+\ldots+n(n+1)=\frac{n(n+1)(n+2)}{3}
$$
11 Pipers Piping

Sheryl Ezze
Sheryl Ezze
Numerade Educator
04:52

Problem 3

The Fibonacci sequence of numbers is defined as follows: The first and second numbers are both 1. After that, each number in the sequence is the sum of the two preceding numbers. Thus, the Fibonacci sequence is as follows:
$$
1,1,2,3,5,8,13,21, \ldots
$$
If $F(n)$ stands for the ith value in the sequence, then this definition can be expressed as
$$
\begin{aligned}
& F(1)=1 \\
& F(2)=1 \\
& F(n)=F(n-1)+F(n-2) \text { for } n>2
\end{aligned}
$$
a. The value of $F$ at position $n$ is defined using the value of $F$ at two smaller positions. Something that is defined in terms of "smaller versions" of itself is said to be recursive, so the Fibonacci sequence is recursive. Using the definition of the Fibonacci sequence, compute the value of $F(20)$.
b. A formula for $F(n)$ is
$$
F(n)=\frac{\sqrt{5}}{5}\left(\frac{1+\sqrt{5}}{2}\right)^n-\frac{\sqrt{5}}{5}\left(\frac{1-\sqrt{5}}{2}\right)^n
$$
Using the formula (and a calculator), compute the value of $F(20)$.
c. What is your opinion on the relative clarity, elegance, and efficiency of the two algorithms (using the definition and using the formula) to compute $F(20)$ ? Would your answer change if you considered $F(100)$ ?
d. If you have access to a spreadsheet, program it to compute successive values of the Fibonacci sequence. (Hint: Enter 1 in cells $\mathrm{A} 1$ and B1, and enter $=A 1+B 1$ in cell $C 1$. Continue along row 1 with a similar formula.) Use this method to compute $F(20)$.
e. Elsewhere on the same spreadsheet, use one cell to enter the value of $n$, and another cell to enter the formula from part (b) above. (Hint: you will need the squareroot function and the
power function.) Use this method to compute $F(20)$.
f. On your spreadsheet, use both methods to compute $F(50)$. Which appears to be more efficient?

Julie Silva
Julie Silva
Numerade Educator
View

Problem 4

A tennis tournament has 342 players. A single match involves 2 players. The winner of a match will play the winner of a match in the next round, whereas losers are eliminated from the tournament. The 2 players who have won all previous rounds play in the final game, and the winner wins the tournament. What is the total number of matches needed to determine the winner?
a. Here is one algorithm to answer this question. Compute $342 / 2=171$ to get the number of pairs (matches) in the first round, which results in 171 winners to go on to the second round. Compute $171 / 2=85$ with 1 left over, which results in 85 matches in the second round and 85 winners, plus the 1 left over, to go on to the third round. For the third round compute $86 / 2=43$, so the third round has 43 matches, and so on. The total number of matches is $171+85+43+\ldots$... Finish this process to find the total number of matches.
b. Here is another algorithm to solve this problem. Each match results in exactly one loser, so there must be the same number of matches as losers in the toumament. Compute the total number of losers in the entire tournament. (Hint: This isn't really a computation; it is a one-sentence argument.)
c. What is your opinion on the relative clarity, elegance, and efficiency of the two algorithms?

Victor Salazar
Victor Salazar
Numerade Educator

Problem 5

We have said that the average number of comparisons needed to find a target value in an n-element list using sequential search is slightly higher than $n 2$. In this problem, we find an exact expression for this average.
a. Suppose the list has an odd number of items, say 15. At what position is the middle item? Using sequential search, how many comparisons
are required to find the middle item? Repeat this exercise with a few more odd numbers until you can do the following: If there are $n$ items in the list and $n$ is an odd number, write an expression for the number of comparisons required to find the middle item.
b. Suppose the list has an even number of items, say 16. At what positions are the two "middle" items? Using sequential search, how many comparisons are required to find each of these? What is the average of these two numbers? Repeat this exercise with a few more even numbers until you can do the following: If there are $n$ items in the list and $n$ is an even number, write an expression for the average number of comparisons required to find the two middle items.
c. Noting that half the items in a list fall before the midpoint and half after the midpoint, use your answer to parts a and $b$ to write an exact expression for the average number of comparisons done using sequential search to find a target value that occurs in an $n$-element list.

Check back soon!
01:15

Problem 6

Here is a list of seven names:
Sherman, Jane, Ted, Elise, Raul, Maki, John
Search this list for each name in turn, using sequential search and counting the number of comparisons for each name. Now take the seven comparison counts and find their average. Did you get a number that you expected? Why?

Ethan Somes
Ethan Somes
Numerade Educator
03:38

Problem 7

The American Museum of Natural History in New York City contains more than 32 million specimens and artifacts in its various collections, including the world's largest collection of dinosaur fossils. Many of these are in storage away from public view, but all must be carefully inventoried.
a. Suppose the inventory is unordered $(I)$ and a sequential search is done to locate a specific artifact. Given that the search is executed on a computer that can do 12,000 comparisons per second, about how much time on the average would the search require?
b. Assuming the inventory is sorted, about how much time would a binary search require?

Khanh Ha
Khanh Ha
Numerade Educator
03:28

Problem 8

In the Flipping Pancakes box, the original algorithm given requires at most $2 n-3$ flips in the worst case. The claim is made that the new algorithm, which requires at most $(5 n+5) / 3$ flips, is a better algorithm. How many pancakes do you need to have before the second algorithm is indeed faster? (Use a calculator or spreadsheet.)

Amany Waheeb
Amany Waheeb
Numerade Educator
02:07

Problem 9

Perform a selection sort on the list 7, 4, 2,9, 6. Show the list after each exchange that has an effect on the list ordering.

Akash M
Akash M
Numerade Educator

Problem 10

The selection sort algorithm could be modified to stop when the unsorted section of the list contains only one number, because that one number must be in the correct position. Show that this modification would have no effect on the number of comparisons required to sort an n-element list.
Exercises 11-14 refer to another algorithm, called bubble sort, which sorts an n-element list. Bubble sort makes multiple passes through the list from front to back, each time exchanging pairs of entries that are out of order. Here is a pseudocode version:
1. Get values for $n$ and the $n$ list items
2. Set the marker $U$ for the unsorted section at the end of the list
3. While the unsorted section has more than one element, do Steps 4 through 8
4. Set the current element marker $C$ at the second element of the list
5. While $C$ has not passed $U$, do Steps 6 and 7
6. If the item at position $C$ is less than the item to its left, then exchange these two items
7. Move $C$ to the right one position
8. Move U left one position
9. Stop

Check back soon!
04:09

Problem 11

For each of the following lists, perform a bubble sort, and show the list after each exchange. Compare the number of exchanges done here and in the Practice Problem at the end of Section 3.3.3.
a. $4,8,2,6$
b. $12,3,6,8,2,5,7$
c. D, B, G, F, A, C, E, H

Bryan Lynn
Bryan Lynn
Numerade Educator

Problem 12

Explain why the bubble sort algorithm does $\Theta\left(n^2\right)$ comparisons on an $n$-element list.

Check back soon!

Problem 13

Suppose selection sort and bubble sort are both performed on a list that is already sorted. Does bubble sort do fewer exchanges than selection sort? Explain.

Check back soon!

Problem 14

Bubble sort can be improved. Smart bubble sort keeps track of how many exchanges are done within any single pass through the unsorted section of the list. If no exchanges occur, then the list is sorted and the algorithm should stop.
a. Write a pseudocode version of the smart bubble sort algorithm.
b. Perform a smart bubble sort on the following list. How many comparisons are required?
$$
7,4,12,9,11
$$
c. Describe the best-case scenario for smart bubble sort on an n-element list. How many comparisons are required? How many exchanges are required?
d. Under what circumstances does smart bubble sort do the same number of comparisons as regular bubble sort?

Exercises 15-17 refer to still another sorting algorithm, called mergesort. Mergesort breaks the list to be sorted into smaller and smaller lists until there is just a bunch of one element (and thus obviously sorted) lists, then assembles the smaller sorted lists back together into larger and larger sorted lists. Here is a pseudocode version:
1. Get values for $n$ and the $n$ list items
2. While the current list has more than 1 item, do Steps 3 through 6
3. Split the list into two halves
4. Sort the first half of the list using mergesort
5. Sort the second half of the list using mergesort
6. Merge the two sorted halves $A$ and $B$ into $a$ new sorted list $C$ by comparing the next two items from $A$ and $B$ and always choosing the smaller value to go into $C$
7. Stop
Mergesort works by using the result of mergesort on two smaller lists, so mergesort, like the Fibonacci sequence in Exercise 3, is a recursive algorithm.
Step 6 in this algorithm deserves an example. Suppose that at one point in running mergesort we have two sorted lists $A$ and $B$, as follows:
$$
A=2,9 \quad B=6,7
$$
To create $C$, we compare 2 from $A$ and 6 from $B$.
Because 2 is smaller, it is removed from $A$ and goes into $C$.
$$
A=9 \quad B=6,7 \quad C=2
$$
Now compare 9 from $A$ and 6 from B. Because 6 is smaller, it is removed from $B$ and goes into $C$.
$$
A=9 \quad B=7 \quad C=2,6
$$
Comparing 9 and 7 results in
$$
A=9 \quad B=\quad C=2,6,7
$$
Finally, let us agree to count comparing 9 to nothing as a legitimate, if trivial, comparison resulting in 9 , which is added to $C$, the final sorted list.
$$
A=B=\quad C=2,6,7,9
$$

Check back soon!
01:05

Problem 15

Show the steps in merging $A$ and $B$ into $C$ where
$$
A=8,12,19,34 \quad B=3,5,15,21
$$

Doruk Isik
Doruk Isik
Numerade Educator
10:39

Problem 16

Use mergesort to sort the list $6,3,1,9$. Count the total number of comparisons, including trivial comparisons.

Bryan Lynn
Bryan Lynn
Numerade Educator
05:29

Problem 17

The work in this algorithm occurs in Step 6 where sorted lists are merged together. The "work unit" is the comparison of two items. If $A$ and $B$ are each of size $k 2$, then $k$ comparisons are needed to merge them into a sorted list $C$ of size $k$. If the original list has 8 elements, then we can picture mergesort as shown on the next page:
Starting from the bottom of the tree, merges occur at three levels (1-element lists merge to 2 -element lists, lists of size 2 merge to lists of size 4 , and lists of size 4 merge to the final list of size 8) and, looking across the entire tree, at each level there are 8 comparisons done to create the level above it. Therefore the total amount of work done is $3 \times 8=24$ comparisons.
a. If the original list has $n$ items, how many levels will the tree have at which merges occur? (Hint: How many times can the number $n$ be cut in half?)
b. If the original list has nitems, how many comparisons occur at each merge level?
c. What is the order of magnitude of mergesort?
d. Does your answer to part c agree with your answer to Exercise 16? Explain.

Narayan Hari
Narayan Hari
Numerade Educator

Problem 18

This exercise discusses a new algorithm to sort an nelement list.
a. A permutation of a list is any arrangement of the list items. For example, 2, 4 and 4,2 are the two permutations of the list 2,4 . Find all permutations of the list $4,3,7$.
b. Given an n-element list, the number of permutations can be counted as follows. There are $n$ positions in the list. Any of the $n$ items can occupy position 1:
a
Once the item for position 1 has been chosen, any of the remaining $n-1$ items can occupy position 2:
$n n-1$
There are then $n-2$ choices for position 3 , etc., until there is only one choice left for the last position-
$\underline{n}, \underline{n-1}, \underline{n-2}, \underline{n-3}, \ldots, 3,2,1$
The total number of permutations is the product $n(n-1)(n-2)(n-3) \ldots(3)(2)(1)$
This value is called $n$ factorial and is denoted by $n$. Compute the value of 3 . How many permutations did you find in part a?
c. Here is a pseudocode description of the new sorting algorithm. It first generates all possible permutations of the list elements and then looks for which of these is the sorted-order permutation.
1. Get values for $n$ and the $n$ list items
2. Set the value of permutation counter i to 1
3. While ( $i \leq n)$ ), do Steps 4 through 6
4. Create a newempty list of size $n$
5. Write the next permutation in this list
6. Increase the value of $i$ by 1
7. Set the value of list counter $j$ to 1
8. Set the value of Sorted to $N O$
9. While $(j \leq n)$ and (Sorted = NO), do Steps 10 through 13
10. Check whether list $j$ is sorted
11. If list $j$ is sorted then
12. Set the value of Sorted to YES
13. Increase the value of $j$ by 1
14. Print list $(j-1)$
15. Stop
To write this algorithm in complete detail, we would need to explain Step 5:
Write the next permutation in this list and Step 10:
Check whether list $j$ is sorted in terms of more primitive operations. For simplicity, assume that each execution of Step 5
is one work unit and that each execution of Step 10 is also one work unit. Explain the best case for this algorithm and give an expression for the total number of work units required. Explain the worst case and give an expression for the total number of work units required.
d. Selection sort is $\Theta\left(n^2\right)$ and the new sorting algorithm is $\Theta(n)$. Fill in the following table, assuming a work rate of 0.0001 seconds per unit of work.
\begin{tabular}{c|c|c|c|c|}
& \multicolumn{4}{|c}{$n$} \\
Order & 3 & 5 & 10 & 20 \\
\hline$n^2$ & & & & \\
$n !$ & & & & \\
\cline { 2 - 5 } & & & &
\end{tabular}
e. Comment on the space efficiency of the new algorithm.

Check back soon!
01:56

Problem 19

Algorithms $A$ and B perform the same task. On input of size $n$, algorithm A executes $0.003 n^2$ instructions, and algorithm B executes $243 n$ instructions. Find the approximate value of $n$ above which algorithm $B$ is more efficient. (You may use a calculator or spreadsheet.)

James Kiss
James Kiss
Numerade Educator

Problem 20

Suppose a metropolitan area is divided into four school districts: $1,2,3,4$. The State Board of Education keeps track of the number of student transfers from one district to another and the student transfers within a district. This information is recorded each year in a $4 \times 4$ table as shown here. The entry in row 1 , column 3 (314), for example, shows the number of student transfers from district 1 to district 3 for the year. The entry in row 1 , column 1 (243) shows the number of student transfers within district 1.
\begin{tabular}{l|llll}
& 1 & 2 & 3 & 4 \\
\hline 1 & 243 & 187 & 314 & 244 \\
2 & 215 & 420 & 345 & 172 \\
3 & 197 & 352 & 385 & 261 \\
4 & 340 & 135 & 217 & 344
\end{tabular}
Suppose there are $n$ school districts, and the Board of Education maintains an $n \times n$ table.
a. Write a pseudocode algorithm to print the table, that is, to print each of the entries in the table. Write an expression for the number of print statements the algorithm executes.
b. Write a pseudocode algorithm to print $n$ copies of the table, one to give to each of the n school district supervisors. Write an expression for the number of print statements the algorithm executes.
c. What is the order of magnitude of the work done by the algorithm in part $b$ if the unit of work is printing a table element?

Check back soon!

Problem 21

Write the data list that results from running the shuffle-left algorithm to clean up the following data. Find the exact number of copies done.
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|}
\hline 3 & 0 & 0 & 2 & 6 & 7 & 0 & 0 & 5 & 1 \\
\hline
\end{tabular}

Check back soon!

Problem 22

Write the resulting data list and find the exact number of copies done by the converging-pointers algorithm when it is executed on the data in Exercise 21.

Check back soon!

Problem 23

Explain in words how to modify the shuffle-left data cleanup algorithm to slightly reduce the number of copies it makes. (Hint: Must item $n$ always be copied?) If this modified algorithm is run on the data list of Exercise 21, exactly how many copies are done?

Check back soon!

Problem 24

The shuffle-left algorithm for data cleanup is supposed to perform $n(n-1)$ copies on a list consisting of nOs (zeros). Confirm this result for the following list:
000000

Check back soon!
02:14

Problem 25

Consider the following sorted list of names.
Arturo, Elsa, JoAnn, John, José, Lee, Snyder, Tracy
a. Use binary search to decide whether Elsa is in this list. What names will be compared with Elsa?
b. Use binary search to decide whether Tracy is in this list. What names will be compared with Tracy?
c. Use binary search to decide whether Emile is in this list. What names will be compared with Emile?

Florencia Cuzmar
Florencia Cuzmar
Numerade Educator
02:30

Problem 26

Use the binary search algorithm to decide whether 35 is in the following list:
$3,6,7,9,12,14,18,21,22,31,43$
What numbers will be compared with 35 ?

RO
Reynald Oliveria
Numerade Educator
04:57

Problem 27

If a list is already sorted in ascending order, a modified sequential search algorithm can be used that compares against each element in turn, stopping if a list element exceeds the target value. Write a pseudocode version of this short sequential search algorithm.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:36

Problem 28

This exercise refers to short sequential search (see Exercise 27).
a. What is the worst-case number of comparisons of short sequential search ona sorted n-element list?
b. What is the approximate average number of comparisons to find an element that is in a sorted list using short sequential search?
c. Is short sequential search ever more efficient than regular sequential search? Explain.

Clarissa Noh
Clarissa Noh
Numerade Educator

Problem 29

Draw the tree structure that describes binary search on the eight-element list in Exercise 25. What is the number of comparisons in the worst case? Give an example of a name to search for that requires that many comparisons.

Check back soon!

Problem 30

Draw the tree structure that describes binary search on a list with 16 elements. What is the number of comparisons in the worst case?

Check back soon!
01:36

Problem 31

We want to find an exact formula for the number of comparisons that binary search requires in the worst case on an n-element list. (We already know the formula is $\Theta(\lg n)$.
a. If $x$ is a number that is not an integer, then $[x]$, called the fioor function of $x$, is defined to be the largest integer less than or equal to $x$. For example, $[3.7]=3$ and $[5]=5$. Find the following values: $[1.2],[2.3],[8.9],[-4.6]$.
b. If $n$ is not a power of 2 , then $\lg n$ is not an integer. If $n$ is between 8 and 16 , for example, then $\lg n$ is between 3 and 4 (because $\lg 8=3$
and Ig $16=4$ ). Complete the following table of values:
\begin{tabular}{ll}
$n$ & $\lfloor\lg n\rfloor$ \\
\hline 2 & 1 \\
3 & \\
4 & 2 \\
5 & \\
6 & \\
7 & \\
8 & 3
\end{tabular}
c. For $n=2,3,4,5,6,7,8$, draw a tree structure similar to Figure 3.19 to describe the positions searched by binary search. For each value of $n$, use the tree structure to find the number of comparisons in the worst case, and complete the following table:
$\frac{n \quad \text { Number of Compares, Worst Case }}{2}$
3
$4 \quad 3$
5
6
$7 \quad 3$
8
d. Comparing the tables of parts $\mathrm{b}$ and $\mathrm{c}$, find a formula involving $\lfloor\lg n\rfloor$ for the number of comparisons binary search requires in the worst case on an nelement list. Test your formula by drawing trees for other values of $n$.

Clarissa Noh
Clarissa Noh
Numerade Educator

Problem 32

Using the tree in Figure 3.19, find the number of comparisons to find each of items $1-7$ in a sevenelement list using binary search. Then find the average. Compare this with the worst case.

Check back soon!
01:32

Problem 33

At the end of Section 3.4.2, we talked about the trade-off between using sequential search on an unsorted list as opposed to sorting the list and then using binary search. If the list size is $n=100,000$, about how many worst-case searches must be done before the second alternative is better in terms of number of comparisons? (Hint: Let $p$ represent the number of searches done.)

James Chok
James Chok
Numerade Educator

Problem 34

Suppose the pattem-matching problem is changed to require locating only the first instance, if any, of the pattern within the text.
a. Describe the worst case, give an example, and give the exact number of comparisons (of a pattern character with a text character) required.
b. Describe the best case, give an example, and give the exact number of comparisons required.

Check back soon!

Problem 35

Suppose you use the brute force (unimproved) algorithm to search for a Hamiltonian circuit in the graph shown here.
a. How many links would such a circuit have?
b. How many different paths would this algorithm examine?
c. If this algorithm is run on a computer where it takes 0.0001 seconds to examine a single path, what is the total time required to examine all paths?
d. On the same computer, what is the approximate total time required to examine all paths in a graph with 12 nodes, each of which has four choices for the next node?

Check back soon!

Problem 36

An Euler path in a graph (named for the Swiss mathematician Leonhard Euler, 1707-1783) is a path that uses each edge of the graph exactly once.
For example, this graph clearly has an Euler path A-B-D-C-A.
a. Decide which of the graphs below have Euler paths, and write out such a path if one exists. (Unlike Hamiltonian circuits, an Euler path need not end at the same node from which it starts, and can go through a given node more than once.)
b. Like the Hamiltonian circuit problem, there is a brute force algorithm to determine whether a graph has an Euler path, again by testing all possibilities. You probably used a variation of this algorithm to solve part $a$. But the Euler path problem has another solution. The degree of a node is the number of edges at that node.
A node with odd degree is an odd node and a node with even degree is an even node. In the first graph of part a, nodes $A$ and $D$ are even (degree 2) and nodes $B$ and $C$ are odd (degree 3). An Euler path exists in any graph with exactly 0 or 2 odd nodes. Which of the graphs at the top of the next page have Euler paths and why? Write out such a path if one exists.
c. Here is a pseudocode version of this algorithm for a graph with n nodes labeled 1 to $n$ :
1. Get values for $n$ and all edges of the graph
2. Set the value of $O d d s$ to 0
3. Set the value of node counter ito 1
(i)
(ii)
(iii)
4. While ( $i \leq n$ ), do Steps 5 through 13
5. Set the value of node counter $j$ to 1
6. Set the value of Degree to 0
7. While $(j \leq n)$, do Steps 8 through 10
8. If anedge $i-j$ exists then
9. Increase Degree by 1
10. Increase $j$ by 1
11. If Degree is an odd number then
12. Increase Odds by 1
13. Increase iby 1
14. If Odds $>2$ then
15. Print the message 'No Euler path' Else
16. Print the message 'Euler path exists'
17. Stop
What is the order of magnitude of this algorithm where the "work unit" is checking whether an edge exists (Step 8)?
d. Is the Euler path problem intractable?

Check back soon!
01:56

Problem 37

At about what value of $n$ does an algorithm that does $100 n^2$ instructions become more efficient than one that does $0.01\left(2^{\prime \prime}\right.$ ) instructions? (Use a calculator or spreadsheet.)

James Kiss
James Kiss
Numerade Educator
03:49

Problem 38

a. An algorithm that is $\Theta(n)$ takes 10 seconds to execute on a particular computer when $n=100$. How long would you expect it to take when $n=500$ ?
b. An algorithm that is $\Theta\left(n^2\right)$ takes 10 seconds to execute on a particular computer when $n$ $=100$. How long would you expect it to take when $n=500$ ?

Mukesh Devi
Mukesh Devi
Numerade Educator

Problem 39

Find an optimal solution to the bin-packing problem described in Section 3.5.

Check back soon!

Problem 40

In the data cleanup problem, we assumed that the items were stored in a list with a fixed number of positions. Each item could be examined by giving its position in the list. This arrangement of data is called an array. Here is an array of four items:
\begin{tabular}{|c|c|c|c|}
\hline 43 & 13 & 55 & 39 \\
\hline 1 & 2 & 3 & 4
\end{tabular}
Another way to arrange items is to have a way to locate the first item and then have each item "point to" the next item. This arrangement of data is called a linked list. Here are the same four items in a linked list arrangement:
To examine any item in a linked list, one must start with the first item and follow the pointers to the desired item.

Unlike arrays, which are fixed in size, linked lists can shrink and grow. An item can be eliminated from a linked list by changing the pointer to that item so that it points to the next item instead.
a. Draw the linked list that results when item 13 is eliminated from the foregoing linked list.
b. Draw the linked list that results when data cleanup is performed on the following linked list.
c. Describe (informally) an algorithm to do data cleanup on a lirked list. You may assume that neither the first item nor the last item has a value of 0 , and you may assume the existence of operations such as "follow pointer" and "change pointer. "If these operations are the unit of work used, show that your algorithm is an $\Theta(n)$ algorithm, where $n$ is the number of items in the list

Check back soon!
03:06

Problem 41

Below is a pseudocode algorithm that prints a set of output values:
1. Get value for $n$
2. Set the value of $k$ to 1
3. While $k$ is less than or equal to $n$, do Steps 4 through 8
4. Set the value of $j$ to one-half $n$
5. While $j$ is greater than or equal to 1 , do Steps 6 through 7
6. Print the value of $j$
7. Set the value of $j$ to one-half its former value
8. Increase $k$ by 1
9. Stop
a. Let $n$ have the value 4 . Write the values printed by this algorithm.
b. Let $n$ have the value 8. Write the values printed by this algorithm.
c. Which of the following best describes the efficiency of this algorithm, where the "work unit" is printing a value?
$\Theta\left(n^2\right) \quad \Theta(n \lg n) \quad \Theta(n) \quad \Theta(\lg n)$
d. How many work units would you expect this algorithm to do if $n=16$ ?

Clarissa Noh
Clarissa Noh
Numerade Educator

Problem 42

Chapter 2 contains an algorithm that finds the largest value in a list of $n$ values.
a. What is the order of magnitude of the largestvalue algorithm, where the "work unit" is comparisons of values from the list?
b. Suppose that you want to find the secondlargest value in the list. Find the order of magnitude of the work done if you use the following algorithm: Sort the list, using selection sort, then directly get the secondlargest value.
c. Suppose that you want to find the secondlargest value in the list. Find the order of magnitude of the work done if you use the following algorithm: Run the largest-value algorithm twice, first to find (and eliminate from the list) the largest value, then to find the second-largest value.

Check back soon!