CS107 - Lab 4

Due Tuesday 10/5 at the beginning of class.

Overview:

Some of the most important algorithms used in computing are in the category "search and sort." These are useful in a variety of settings, such as web search, alphabetic sorting of a list of names, etc. They are also related, since the most efficient search algorithms occur on lists that are sorted. This lab provides an opportunity to explore the functioning of search and sort algorithms, as well as their efficiency. The readings and problems for this assignment are in Chapter 3 of your text.


Course software

Following the same steps that you used for Assignment 2, double-click the icon csci107 - Invitation (OSX) that you copied to the desktop. The following menu should appear:



The buttons Search Animator, Sort Animator and Sort Timer will be useful for this assignment. When you select either of these, you will be able to animate the behavior of several different algorithms in Chapter 3, as well as empirically measure the efficiency of different sorting algorithms on different amounts of data.

For example, select Algorithm -> Selection Sort in the Sort Animator and then select the Run button. You may Reset and Rerun this exercise several times to get a feeling for how the algorithm works. To count the number of exchanges and comparisons in a particular selection sort, you can repeatedly click the Step button and keep track of the number of times steps 9 and 6 are repeated, respectively.

Now opn the Sort Timer and select Algorithm -> Selection Sort and Points -> 128, and then select Run. Note that this gives the running time for selection-sorting 128 data points, and it graphically displays the behavior of the data being rearranged as the algorithm proceeds.

These animators will be useful for answering some of the questions below.


Designing Algorithms

Complete exercises 17, 18, 21, 22, 25, 26, at the end of chapter 3 of your textbook (pages 119-120). In addition solve the following problems:

Problem 1 Write an algorithm that reads a number n and a list of n-1 distinct integers from the user in the range 1,2,...,n and figures out what is the missing number. For instance, if the user types in

6
1 5 3 2 6
your algorithm should print
The missing number is 4

Analyze the worst-case and best-case running time of your algorithm.

Problem 2 Write an algorithm that reads a number n and a list of n numbers from a user and computes and prints the median element(s) in the list (the median is an element which has an equal number of elements smaller and larger than it). For instance for the list

 4 3 1 2 5 7  6
the median is 4. For the list
 5 4 3 8 6 7 1 2
the medians are 4 and 5.

You can use any of the algorithms studied in class as a black box, without writing the code for them. Analyze the worst-case and best-case running time of your algorithm.

Problem 3 Write an algorithm that evaluates a polynomial at a point. The algorithm should read in from the user a list of n+1 coefficients, a0, a1, a2,...,an and a value x. The algorithm should compute and print a0 + a1x+a2x2 ...+anxn.

Analyze the worst-case and best-case running time of your algorithm.



What to turn in:

Be sure to justify your answers! You may do this work either with a word processor or by hand. If you type your solutions, bring a hard copy of your assignment and hand it in class. If you write by hand, do your best at writing legibly and leave space between problems.

You may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on Tuesday, at the beginning of class.

Once you are finished in the lab, if you saved any documents in yout collaboration personal directory, be sure to drag the csci107 icon to the Trash (this step disconnects you from the server and prevents someone else, who may use this iMac later in the day, from accidentally accessing files in your personal folder) and log out!