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

Cairo University

Faculty of Computers and Artificial Intelligence


Midterm Exam
Department: CS
Course Name: Algorithms Date: 30-11-2021
Course Code: Duration: 1 hours
Instructor(s): Dr/Basheer Total Marks: 20
Youssef

Q1 Assume you have hash table T with size = 10 Add these values
(0,30,1,55,16,28,13,9 ,26) in it using the following methods,
[10 point 2.5 for each one]
1- Linear probing using hash function h(key) = key % 10
index Value
0 0
1 30
2 1
3 13
4
5 55
6 16
7 26
8 28
9 9

2- Quadratic Probing using hash function h(key) = key % 10


0,30,1,55,16,28,13,9 ,26)
index Value
0 0
1 30
2 1
3 13
4
5 55
6 16
7 26
8 28
9 9

3- Double hashing Assume the main hash function h1(key) = key % 10 And
the second hash function h2(key) = 5 – (key % 5).
hi(key)=(h1(key)+i*(h2(key)) mod 10,

0,30,1,55,16,28,13,9 ,26)

55 cannot inserted

Index Value
0 0
1 1
2
3 13
4 26
5 30
6 16
7
8 28
9 9

4- Chaining.
0,30,1,55,16,28,13,9 ,26)

Index 0 30
0 1
1
2
3 13
4
5 55
6 16 26
7
8 28
9 9
Q2 How would you optimally calculate p^k, where k is a non-negative integer?
What is the complexity of the solution?[5 points]

Answer using divide and conquer method


T(1) = 1
T(n)=T(n/2) +1
Complexity = lgn
Q3 Suppose we are comparing implementations of insertion sort and merge sort on
the same machine. For inputs of size n, insertion sort runs in 8n^2 steps, while
mergesort runs in 64nlgn steps. For which values of n does insertion sort beat
merge rt?[2.5 points]
Answer
8n^2 < 64nlgn
n<8lgn
If students use base 2 will be 43 or say between (32 and 64)
Base 10 will 6
Q4 Solve the following recurrence using master method.
T(n)= 2T(n/4) + √𝑛 [2.5 points]
Answer
a =2 b=4 d=½ c= ½
n^1/2lgn or √𝑛𝑙𝑔𝑛

You might also like