0% found this document useful (0 votes)
15 views

Assignment 2

This document outlines 4 exercises for an assignment on algorithms and data structures. Exercise 1 involves analyzing the count-sort algorithm, Exercise 2 involves designing algorithms for tree traversal and node depth calculation, Exercise 3 involves illustrating sorting algorithms, and Exercise 4 involves writing a program to simulate a "hot potato" game and analyzing its time complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment 2

This document outlines 4 exercises for an assignment on algorithms and data structures. Exercise 1 involves analyzing the count-sort algorithm, Exercise 2 involves designing algorithms for tree traversal and node depth calculation, Exercise 3 involves illustrating sorting algorithms, and Exercise 4 involves writing a program to simulate a "hot potato" game and analyzing its time complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSI2114 - Spring 2006 - Assignment 2

Due: June 8, 2006 by 5 pm (in Box # 5)

Exercise 1. [15 points]


Let S be a sequence containing pairs (k,e) where e is an element and k is its key. There is
a simple algorithm called count-sort that will construct a new sorted sequence from S
provided that all the keys in S are different from each other. For each key k, count-sort
scans S to count how many keys are less than k. If c is the count for k then (k,e) should
have rank c in the sorted sequence.
a) Give the pseudo-code for count-sort.
b) Determine the number of comparisons made by count-sort. What is its running time?

c) As written, count-sort only works if all of the keys have different values. Explain
how to modify count-sort to work if multiple keys have the same value.

Exercise 2. [10 points]


a) Give an O(n)-time algorithm for computing the depth of each node of a tree T, where
n is the number of nodes of T. Assume the existence of methods setDepth(v,d) and
getDepth(v) that run in O(1) time.

b) Design an algorithm for the following operation for a binary tree T of size n, and
analyze its worst-case running time. Your algorithms should avoid performing
traversals of the entire tree.
preorderNext(v): return the node visited after v in an preorder traversal

Exercise 3. [15 points]


a) Ilustrate the execution of the selection-sort algorithm on the following input
sequence: (26, 35, 16, 24, 10, 23, 19, 13, 39, 15).

b) Ilustrate the execution of the insertion-sort algorithm on the following input


sequence: (26, 35, 16, 24, 10, 23, 19, 13, 39, 15).

c) Ilustrate the execution of the heap-sort algorithm on the following input sequence:
(26, 35, 16, 24, 10, 23, 19, 13, 39, 15).

Exercise 4. [20 points]


In the children’s game “hot potato” a group of n children sit in a circle passing an object,
called the “potato”, around the circle (say in a clockwise direction). The children
continue passing the potato until a leader rings a bell, at which point the child holding the
potato must leave the game, and other children close up the circle. The process is then
continued until there is only one child remaining, who is declared the winner. Suppose
the leader always rings the bell immediately after the potato has been passed k times.
Write a Java program which uses Sequence ADT to implement this game: It takes as an
input n and k and outputs the sequence of children (assume they have names 0, 1, . . ., n-1
leaving the game. (The game starts with the child 0 holding the potato.) For example, the
output for n = 5, k = 3 should be ‘3,1,0,2,4’.

What is the running time of your method (ignore input and output operations), in terms of
n and k, assuming the sequence is implemented with a doubly linked list? What if the
sequence is implemented with an array?

Note that the main objective of this question is to implement the solution for the “hot
potato” problem, not to implement Sequence – you can use the implementation of
Sequence from the textbook’s web site (but make sure it works!) .

You might also like