You are on page 1of 1

CSI323: Algorithms

University of Botswana August 29, 2023


Lecturer: T.Z. Nkgau Homework #1

Homework #1
Due date: September 11, 2023 at 10am
Instructions

• You may work in groups of up to three people.

• All implementations must be properly documented. Poorly documented implementations will


be penalized.

• There must be no inter-group collaboration.

• Names (and student numbers) of all group members must appear on all files submitted. This
requirement includes source code files.

• You are to submit all implementations as source code files (.java or .ram) and all written
solutions as PDF.

1. [Sedgewick & Wayne] 1.1.31, 1.2.1

2. Implement all the algorithms in the paper by Daescu and Luce.

3. An array A[1 : n of integers is arranged such that at first the integers are increasing then they
decrease. This means that there is an index k such that A[1] < A[2] < · · · < A[k − 1] < A[k]
and A[k] > A[k + 1] > · · · > A[n − 1] > A[n]. For k, A[k + 1] < A[k] < A[k − 1]. In fact, A[k]
is the maximum element in the array.

(a) Give an array with 6 integers as an example.


(b) Give a naive (brute force) algorithm to return the index of the maximum element in
such arrays.
(c) Design a divide and conquer algorithm to return the index of the maximum element in
such arrays.

4. Write a RAM program to compute 2n when n ≥ 0 is the input. What is the uniform cost
time and space complexity of your RAM program in terms on n?

5. A MoveToFront data type stores a sequence of elements. Its functionality is specified by


the following the following API.

public class MoveToFront<E>


MoveToFront() Create an empty MoveToFront data structure
void add(E item) Add the item at the front of the current sequence
E get(int i) Get the item at index i. First item in the sequence is at index 0
void moveToFront(int i) Move the item at index i to the front of the sequence

Implement the MoveToFront data type making the methods as efficient as possible.

You might also like