Chapter C

Comp3303: Algorithms Worksheet
[CBPrice. 17-14-16]
Color Coding for this Worksheet
Information
Instructed Learning
Inquiry based Learning
Purpose
(a) To understand the concept (“idea”) of an algorithm (b) To be able to estimate the execution time of
some common algorithms (c) To appreciate “tractable” and “intractable” problems as well as the existence
of problems that cannot be solved.
Activities
1
Computational Thinking (1)
(a) You have three colored balls in three containers as shown below on the left. You must move the
balls to arrive at the arrangement on the right. There are four rules
Rule 1. You can only move one ball at a time
Rule 2. The balls must always be inside a container
Rule 3. You can stack balls on top of each other
Rule 4. You can only move the top ball in a stack
Show each stage in your solution.
(b) Now repeat for the case shown below
2
Computational Thinking (2)
(a) Rework the problem (a) above but where you have two balls of the same color in each
container on the left diagram and you must end up with two balls of the same color in each
container on the right.
(b) Can you write an “algorithm” to describe how to handle this case of two balls per container?
(c) Does your algorithm work with 3 or more balls per container?
3
Computational Thinking (3)
You have three empty containers and four black balls. How can you place the balls into the
containers so that each container contains an odd number of balls?
4
The Sequential Search Algorithm.
Let’s take the following list of names and apply the sequential search algorithm using pencil and
paper. (Note you can refer to each name using only its first letter).
Alan, Betty, Carol, David, Edward, Fred, George
(a) How many comparisons do you need to find Edward?
(b) What is the smallest number of comparisons you may need to find a given name?
(c) What is the largest number of comparisons you may need to find a given name?
(d) How many comparisons do you need to locate Zara ?
(e) Write pseudocode for the sequential search algorithm.
5
Binary Tree Search Algorithm. Construct a binary tree using the names in Task1. You may use the
first letter of the name. The binary tree should have the following structure; you must decide which
name goes where.
Now answer the following questions
(a) How many comparisons do you need to find Edward?
(b) What is the smallest number of comparisons you may need to find a given name?
(c) What is the largest number of comparisons you may need to find a given name?
(d) How many comparisons do you need to locate Zara?
(e) Compare your answers to those from the sequential search. How do both algorithms scale with
N?
6
Measuring Execution Times on the Pentium
You are given two executable files, “Quicksort.exe” and “Bubblesort.exe”. These programs run both
sorting algorithms for a variable number of random numbers and record the time each run takes.
The time is given via two ways (i) using the clock() function of C, and also by counting the number
of machine cycles. Run both algorithms for various numbers of numbers and record the times
taken and the number of machine cycles. For bubble sort you may want to try n from, 1000 to
150,000, for quicksort, I suggest 50,000 to 20,000,000. There’s already a hint! Plot the results from
each algorithm using Excel on individual graphs. Now plot the data available for the same n from
each algorithm on the same graph. What do you notice?
7
Finding the Heavier Coin
You have 8 coins one of which is heavier and a pan balance.
(a) Start by placing 4 coins on the left pan and 4 coins on the right pan. Work out, using the
technique shown in class how to find the heavier coin. What is the smallest number of weighing’s
you may need? What is the largest?
(b) Now solve this problem by dividing the 8 coins into two groups of 4. You weigh each group
separately. Now what is the smallest number of weighing’s you may need? What is the largest?
(c) [Hard] Now solve this problem by dividing the 8 coins into two groups of 3 and one group of 2.
Start by weighing the two groups of 3. Now what is the smallest number of weighing’s you may
need? What is the largest?