Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Algorithms and data structures 2

Task 1
1. The best case input is R0(7, [7,6,2,4,9], [5,3,8,1], 9). A1 is the first
array the algorithm is going to check and 7 is the first number in the
array so the search will be over after the first comparison. The worst
case input is when the number is not in A1 or A2 because the
algorithm will have to check both of the arrays and then return -1.
2. The best case running time will be 1. It will have a constant running
time because the algorithm will only have to check the first integer in
A1 so the for loop only does one iteration.
The worst case running time is (f(N)) would be Ceiling(N/2) + C +
floor(N/2) + C f(N) = N + 2C
3. The growth function for the best case running time is O(1). The
growth function for the worst case running time is O(N).
4. The Theta notation for the best case is Theta(1) as it is constant
time.
The Theta notation for the worst case is Theta(N).
0.5N < f(N) <2N
C1 = 0.5, c2 = 2, n0 = 1
Task 2
1. The best case and worst case inputs will be the same because
create makes a new array which will take a constant amount of time
however big the length of A is. Finding the sum of the integers by
calling function sum and storing it in b[0] will also take the same
amount of time.
2. T(N) = 2T(N/2) + k
a = 2 b = 2 logba = 1 c = 0
If f(n) = θ(Nc) where c < logba: T(N) = θ(nlogba)
T(n) = θ(n)

Task 3
1.

2. The worst case input will be if the key in not in the array and the
algorithm returns -1. The worst case running time is O(n) because the
algorithm will check every integer to see if it adds up to b[j] and to
find the key.

3. Not all errors will be detected because the algorithm does not
check if the numbers in array A are still the same. If the numbers
change but they still add up to the integer in B the algorithm will not
detect that.

Task 4
1.

2. If there are repeated integers then they would be hashed to the


same place so separate chaining can be used as a collision resolution.
Separate chaining is usually implemented using linked lists. Each
element of the hash table is a linked list. If there is a repeated integer
it would be stored in the same list as the other integers that were
hashed to the same place.
If there are more integers than there are index’s then the hash table
could be extended so that the load factor will become lower and
linear probing could be used to keep the integer in the next free
index.

Task 5
In this setting there will be an array of length n storing non-negative
numbers. There will be two new arrays created with the same
integers from the first array. To find the mid-point of the array a
function will add up all of the integers and divided that by the
number of integers in the array. The numbers that are less than the
mid-point go to the first array otherwise if they are bigger then they
are stored in the second array. The function will take the key, A1, A2
the middle number in the array and the size of the array as inputs.

The function will first compare the key with the middle element in
the array. If it is less than or equal to the element the function will
use a for loop to check each integer in A1 until the key is found.
If the key is bigger then the function will search for it in A2. If the key
has been found the function will return the index. If the key has not
been found in both of the arrays -1 will be returned, or if the integers
that are in A are different from A1 or A2 there is a data storage error
and -2 will be returned.
The best case input is if the key is smaller than the mid and it is
stored in the first index in A1 because the function will search that
algorithm first so the worst case running time is O(1). The worst case
input is if the key is not in the array because the function will have to
search both of the arrays and then return -1 when it is not found. The
worst case running time is O(n).

You might also like