You are on page 1of 10

Data Structures & Algorithms 1st Year Sheet #8 - Solution

Cairo University
Faculty of Engineering
Computer Engineering Department
Data Structures and Algorithms
Sheet #8 - Solution
Searching, Hashing and Sorting

A. Searching

1. Using sequential search, search for the following keys: 20, 15, 21 and 31 in the following list. Commented [E1]:
Show the comparison order and number of comparisons. Best case: O(1)
Worst case: O(n)
20 25 19 22 15 10 30 21 Key =20 ➔ one comp. (20)
Key =15 ➔ 5 comp (20,25,19,22,15)
Key = 21 ➔ 8 comp (all array in order)
Key =31 ➔ when key is not in the arrya you always make n
comp
2. Using binary search, search for the following keys: 26, 41, 23, 30, 5, and 50 in the following list.
Commented [E2]:
Show the comparison order and number of comparisons. At each loop iteration, including the last, Best case: O(1)
show the contents of first, last, and mid. Worst case: O(logn)

Index 0 1 2 3 4 5 6 7
item 10 17 23 26 29 31 40 41

Solution: showing search for key = 41 as an example:


Iteration First Last mid Arr[mid] Comments Commented [M. Ismail3]:
1 0 7 3 26 Key>Arr[mid] ➔ set First = mid+1 Mid=(First+Last)/2
2 4 7 5 31 Key>Arr[mid] ➔ set First = mid+1
3 6 7 6 40 Key>Arr[mid] ➔ set First = mid+1
4 7 7 7 41 Key = Arr[mid] ➔ key found

3. Show the best and worst case of the following algorithms:


a. Sequential Search Commented [E4]:
Best case: O(1)
Worst case: O(n)
b. Sentinal Search
Commented [E5]:
[Important]
Same complexity as sequential but less number of
c. Binary Search comparisons because it doesn’t check or array boundary
but still same complexity class.
Commented [E6]:
d. Ordered List Sequential Search Best case: O(1)
Worst case: O(logn)

4. Write the recursive algorithm of binary search. Commented [E7]: Same complexity as sequential but less
number of element comparisons because of using the
property of being sorted.
B. Hashing Check the algorithm in the slides.

1. Using the hash function h (key) = key mod 11,


a. Apply the following collision resolution techniques to insert the following records.
30 20 56 75 31 19 22
i. separate chaining Commented [E8]: Linked list
ii. Linear probing Commented [E9]: Open addressing
iii. Quadratic probing
Commented [E10]: Open addressing
iv. Pseudorandom method with a = 3 and b = 5.

b. For each technique of the above answer these questions:


i. What is the load factor of this hash table? Commented [E11]: no. of filled elements / table max size
ii. What is the number of comparisons needed to search for: 30, 31, 41, 19and 44?

CMP N102 1/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

Solution of problem B-1:


Home address of each key:
key 30 20 56 75 31 19 22
h(key) 8 9 1 9 9 8 0

Separate Chaining (Linked List):


Index 0 1 2 3 4 5 6 7 8 9 10
value Linked list Linked list Linked list Linked list
containing: containing: containing: containing:
22-># 56-># 30->19-># 20->75->31->#

i. What is the load factor of this hash table? Commented [E12]: = 4/11
ii. What is the number of comparisons needed to search for: 30, 31, 41, 19 and 44? Commented [E13]:
Key 30:
-home location = h(30) = 8
-comparisons with: 30 (1st elem in linked list) → found
Key 31:
-home location = h(31) = 9
-comparisons with: 20, 75, 31 (3rd elem in linked list) → found
Key 41:
-home location = h(41) = 8
Linear Probing: -comparisons with: 30, 19 → not found
Index 0 1 2 3 4 5 6 7 8 9 10
value 31 56 19 22 30 20 75
i. What is the load factor of this hash table? Commented [E14]: = 7/11
ii. What is the number of comparisons needed to search for: 30, 31, 41, 19 and 44? Commented [E15]:
Key 30:
-home location = h(30) = 8
-comparisons with: 30 → found
Key 31:
-home location = h(31) = 9
-comparisons with: 20, 75, 31 → found
Key 41:
Quadratic Probing: -home location = h(41) = 8
Index 0 1 2 3 4 5 6 7 8 9 10 -comparisons with: 30, 20, 75, 31, 56, 19, 22, empty→ not
value 22 56 19 31 30 20 75 found
i. What is the load factor of this hash table? Commented [E16]: = 7/11
ii. What is the number of comparisons needed to search for: 30, 31, 41, 19 and 44? Commented [E17]:
Key 30:
-home location = h(30) = 8
-comparisons with: 30 → found
Key 31:
-home location = h(31) = 9
-comparisons with: 20, 75, 31 → found
Key 41:
-home location = h(41) = 8
-comparisons with: 30, 20, 19, 22, empty(loc 5) → not found
Pseudorandom method with a = 3 and b = 5.
Commented [E18]:
Index 0 1 2 3 4 5 6 7 8 9 10 - address = (old address * 3 + 5) % table size
value 22 56 31 19 30 20 75 Note: old address is not the home address. It’s the address of
the last collision.

-No of comparisons of key:


30 → 1, 20 → 1, 56 →1, 75 → 2, 31 →3, 19 → 2, 22 → 1

-Search Key 41:


-home location = h(41) = 8
-comparisons with: 30, 19, 19, empty(loc 4) → not found

2. Using the modulo-division method and linear probing, store the keys shown below in an array with
19 elements. How many collisions occurred? What is the load factor of the hash table? Commented [E19]:
H(k) = k mod 19
224562 137456 214562 140145 214576 162145 144467 199645 234534

CMP N102 2/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

C. Sorting

1. For the following data, apply the following algorithms to sort data in ascending order. Commented [E20]: Ascending or descending is always
Note: For all algorithms the first part sorted is that on the LEFT. considered starting from the beginning of array arr[0] no
matter if the question says sorted part starts at LEFT or
Show the result and number of inversions after each pass. RIGHT.
a. Selection sort Ascending: 1 2 3 4
b. Insertion sort Descending: 4 3 2 1
c. Bubble sort Commented [M. Ismail21]: The number of inversions is
d. Shell the number of out-of-order pairs
e.g. 5,8 is not an inversion
but 5,3 is an inversion
5 8 3 10 1 7 4 6 2 9

Solution: (Sorted items will be highlighted in Blue) Commented [M. Ismail22]:


Note: the sorted part (blue) is on LEFT as specified in the question. For straightforward algorithms, we will provide only few
solution passes.
In exam they are usually asked for few passes but in the sheet
Selection Sort: students should solve all problems to the end
Inversions
Initial 5 8 3 10 1 7 4 6 2 9 23
After 20
1 8 3 10 5 7 4 6 2 9
Pass 1
Pass 2 1 2 3 10 5 7 4 6 8 9 9
Pass 3 1 2 3 10 5 7 4 6 8 9 9
Pass 4 1 2 3 4 5 7 10 6 8 9 4

Insertion Sort: (1st element in initial array is considered sorted)


Note: In insertion sort 1st pass starts from 2nd element (not 1st element).
Inversions
initial 5 8 3 10 1 7 4 6 2 9 23
After 23
5 8 3 10 1 7 4 6 2 9
Pass1
Pass2 3 5 8 10 1 7 4 6 2 9 21
Pass3 3 5 8 10 1 7 4 6 2 9 21
Pass4 1 3 5 8 10 7 4 6 2 9 17

Bubble Sort:
Note: In the original bubble sort algorithm, processing the array is from right to left but the sorted part is
at left i.e. the 1st pass gets the smallest element on left. In this question, it said the first sorted part
should be on left, so we’ll use the original bubble sort algorithm.

initial 5 8 3 10 1 7 4 6 2 9
After
1 5 8 3 10 2 7 4 6 9
Pass1
Pass2 1 2 5 8 3 10 4 7 6 9
Pass3 1 2 3 5 8 4 10 6 7 9
Pass4 1 2 3 4 5 8 6 10 7 9

CMP N102 3/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

Shell Sort
The highlighted items are those involved in the sort at current step
Initial number of inversions = 4+6+2+6+3+1+1 = 23

Increment k = size/2 = 10/2 = 5


initial 5 8 3 10 1 7 4 6 2 9
After
5 8 3 10 1 7 4 6 2 9
(this is one pass)

step1
Step2 5 4 3 10 1 7 8 6 2 9
Pass 1

Step3 5 4 3 10 1 7 8 6 2 9
Step4 5 4 3 2 1 7 8 6 10 9
End
of 5 4 3 2 1 7 8 6 10 9
pass1
No. of inversions after pass1 = 4+3+2+1+1+1+1 = 13

Increment k = kold/2 = 5/2 = 2


initial 5 4 3 2 1 7 8 6 10 9
After
3 4 5 2 1 7 8 6 10 9
step1
Step2 3 2 5 4 1 7 8 6 10 9
(this is one pass)

Step3 1 2 3 4 5 7 8 6 10 9
Step4 1 2 3 4 5 7 8 6 10 9
Pass 2

Step5 1 2 3 4 5 7 8 6 10 9
Step6 1 2 3 4 5 6 8 7 10 9
Step7 1 2 3 4 5 6 8 7 10 9
End
of 1 2 3 4 5 6 8 7 10 9
pass2
No. of inversions after pass2 = 1+1 = 2

Increment k = kold/2 = 2/2 =1


initial 1 2 3 4 5 6 8 7 10 9
After
1 2 3 4 5 6 8 7 10 9
step1
(this is one pass)

Step2 1 2 3 4 5 6 8 7 10 9
Step3 1 2 3 4 5 6 8 7 10 9
Pass 3

Step4 1 2 3 4 5 6 8 7 10 9
Step5 1 2 3 4 5 6 8 7 10 9
Step6 1 2 3 4 5 6 8 7 10 9
Step7 1 2 3 4 5 6 7 8 10 9
Step8 1 2 3 4 5 6 7 8 10 9
End
of 1 2 3 4 5 6 7 8 9 10
pass3
No. of inversions after pass3 = 0 ➔ list is sorted

CMP N102 4/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

2. Repeat question 1 algorithms to sort data in descending order.

Solution:

Selection Sort: solved as an example for descending order


initial 5 8 3 10 1 7 4 6 2 9
After
10 8 3 5 1 7 4 6 2 9
Pass 1
Pass 2 10 9 3 5 1 7 4 6 2 8
Pass 3 10 9 8 5 1 7 4 6 2 3
Pass 4 10 9 8 7 1 5 4 6 2 3

3. Repeat question 1 but with make the first part sorted is that on the RIGHT.

Solution:

Selection Sort: (right-to-left)


initial 5 8 3 10 1 7 4 6 2 9
After
5 8 3 9 1 7 4 6 2 10
Pass 1
Pass 2 5 8 3 2 1 7 4 6 9 10
Pass 3 5 6 3 2 1 7 4 8 9 10
Pass 4 5 6 3 2 1 4 7 8 9 10

Insertion Sort: (right-to-left)


Initial 5 8 3 10 1 7 4 6 2 9
After
5 8 3 10 1 7 4 6 2 9
Pass1
Pass2 5 8 3 10 1 7 4 2 6 9
Pass3 5 8 3 10 1 7 2 4 6 9
Pass4 5 8 3 10 1 2 4 6 7 9

Bubble Sort: (right-to-left)


Inversions
initial 5 8 3 10 1 7 4 6 2 9 23
After 5 3 8 1 7 4 6 2 9 10 16
Pass1
Pass2 3 5 1 7 4 6 2 8 9 10 10
Pass3 3 1 5 4 6 2 7 8 9 10 6
Pass4 1 3 4 5 2 6 7 8 9 10 3

CMP N102 5/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

Shell Sort: (right-to-left)


The highlighted items are those involved in the sort at current step
Initial number of inversions = 4+6+2+6+3+1+1 = 23

Increment k = size/2 = 10/2 = 5


initial 5 8 3 10 1 7 4 6 2 9
After
5 8 3 10 1 7 4 6 2 9
step1
(this is one

Step2 5 8 3 2 1 7 4 6 10 9
Pass 1

pass)

Step3 5 8 3 2 1 7 4 6 10 9
Step4 5 4 3 2 1 7 8 6 10 9
End of
5 4 3 2 1 7 8 6 10 9
pass1
No. of inversions after pass1 = 4 + 3 +2 +1 + 1+ 1 = 12

Increment k = kold/2 = 5/2 = 2


initial 5 4 3 2 1 7 8 6 10 9
After
3 4 5 2 1 7 8 6 10 9
step1
(this is one pass)

Step2 3 4 5 2 1 7 8 6 10 9
Step3 3 4 5 2 1 6 8 7 10 9
Pass 2

Step4 3 4 5 2 1 6 8 7 10 9
Step5 3 4 5 2 1 6 8 7 10 9
Step6 3 4 1 2 5 6 8 7 10 9
Step7 3 2 1 4 5 6 8 7 10 9
End of
1 2 3 4 5 6 8 7 10 9
pass2
No. of inversions after pass2 = 1+1 = 2

Increment k = kold/2 = 2/2 =1


initial 1 2 3 4 5 6 8 7 10 9
After
1 2 3 4 5 6 8 7 9 10
step1
(this is one pass)

Step2 1 2 3 4 5 6 8 7 9 10
Step3 1 2 3 4 5 6 7 8 9 10
Pass 3

Step4 1 2 3 4 5 6 7 8 9 10
Step5 1 2 3 4 5 6 7 8 9 10
Step6 1 2 3 4 5 6 7 8 9 10
Step7 1 2 3 4 5 6 7 8 9 10
Step8 1 2 3 4 5 6 7 8 9 10
End of
1 2 3 4 5 6 7 8 9 10
pass3
No. of inversions after pass3 = 0 ➔ list is sorted

4. Repeat question 2 but make the first part sorted is that on the RIGHT.

Solution:
Same concept as question 3 but here in descending order.

CMP N102 6/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

5. For the following data, apply the following algorithms to sort data in ascending order. Show result
after each pass.
a. Heap
b. Quick (assume a pass is one recursive call (working on one pivot per pass) and the pivot
element of a range is the first element on the left of that range)
index 0 1 2 3 4 5 6 7 8 9
Initial
21 50 14 19 30 17 40 27 12 13
array
Solution:
Heap Sort:
Step 1. Convert the given array into a heap (using Heapify or Build Heap algorithm)
Build Heap’s algorithm (mentioned in the slides):
a. Start at index 1 (call this index -> walker)
b. Reheap up the walker element
c. Increment walker index
d. Repeat step b and c till walker reaches the end of the array
We will show conversion steps in the array and for each step we highlight the elements involved.
The walker of the next iteration will be highlighted in yellow. The swapped elements will be
highlighted in green.
index 0 1 2 3 4 5 6 7 8 9
Initial
21 50 14 19 30 17 40 27 12 13
array
After
50 21 14 19 30 17 40 27 12 13
Step1
Step2 50 21 14 19 30 17 40 27 12 13
Step3 50 21 14 19 30 17 40 27 12 13
Step4 50 30 14 19 21 17 40 27 12 13
Step5 50 30 17 19 21 14 40 27 12 13
Step6 50 30 40 19 21 14 17 27 12 13
Step7 50 30 40 27 21 14 17 19 12 13
Step8 50 30 40 27 21 14 17 19 12 13
Final
50 30 40 27 21 14 17 19 12 13
Heap

Step 2. Repeat the following for the n elements


a. Exchange root with last element
b. Decrement array size (n--)
c. Reheap down the new heap of size n-1
Again, we will show the steps on the array itself not on a heap tree.
After each pass, sorted elements are highlighted in blue. The elements swapped in the reheap down
will be highlighted in green.
index 0 1 2 3 4 5 6 7 8 9
Initial heap 50 30 40 27 21 14 17 19 12 13
Pass After Swap 50,13 13 30 40 27 21 14 17 19 12 50
1 After Reheap down 40 30 17 27 21 14 13 19 12 50
Pass After Swap 40,12 12 30 17 27 21 14 13 19 40 50
2 After Reheap down 30 27 17 19 21 14 13 12 40 50
Pass After Swap 30,12 12 27 17 19 21 14 13 30 40 50
3 After Reheap down 27 21 17 19 12 14 13 30 40 50
Pass After Swap 27,13 13 21 17 19 12 14 27 30 40 50
4 After Reheap down 21 19 17 13 12 14 27 30 40 50
And so on until the whole array is sorted

CMP N102 7/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

Quick Sort:
Note: The question specified that a pass is one recursive call and the following solution is according to this
assumption.
A pass may be specified in a different way in the exam and you must follow it and solve accordingly. For
example, it may consider a pass by positioning a pivot per each existing array partition, so 1st pass as usual will
result in 2 partitions (left partition and right partition of pass 1’s pivot) and 2nd pass will apply the algorithm on
these 2 partitions taking the pivot of each partition as the left element of it.
Common Mistakes in Quizzes:
• Considering the pass by 1 swap of large and small elements
• Putting the pivot in its correct position but the elements on the left and right positions are in any
positions NOT as the positions that will result from following the algorithm steps and where you should
make the swaps of large and small elements.
• Not putting the pivot in its correct position after a pass

Solution according to the assumption mentioned in the question above:


We will make the pivot the first element and move with two indices (Low “L” and Hi “H”) swapping items
until we place pivot at its correct position.
The recursion executes the algo on left part then right part
pp ➔ Pivot point, L➔ Low index, H➔ Hi index
Sorted element are highlighted in blue

After First Quick Sort Function Call (After pass1)


Initially L = 0, H=9, pp=0
0 1 2 3 4 5 6 7 8 9
Initial
array 21 50 14 19 30 17 40 27 12 13

Iteration1 pp L H
Move L,H 21 50 14 19 30 17 40 27 12 13
A[L]>A[pp] A[H]<A[pp]
Swap L,H 21 13 14 19 30 17 40 27 12 50

Iteration 2 pp L H
Move L,H 21 13 14 19 30 17 40 27 12 50
A[L]>A[pp] A[h]<A[pp]
Swap L,H 21 13 14 19 12 17 40 27 30 50

Iteration 3 pp H L
H<L➔
swap
pp,H 17 13 14 19 12 21 40 27 30 50

Notice how all items < 21 are now on its left and all items > 21 are on its right and 21 itself has been
placed in its correct position.

CMP N102 8/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

After 2nd Quick Sort Function Call (After pass2)


Initially L=0, pp=0, H=4

0 1 2 3 4 5 6 7 8 9
Initial
array 17 13 14 19 12 21 40 27 30 50

After
Iteration1 pp L H
Swap L,H 17 13 14 12 19 21 40 27 30 50

After
Iteration 2 pp H L
H<L➔
swap pp,H 12 13 14 17 19 21 40 27 30 50

After 3rd Quick sort Function call (After pass3)


Initially L=0, pp=0, H=2

0 1 2 3 4 5 6 7 8 9
Initial
array 12 13 14 17 19 21 40 27 30 50

After
Iteration1 pp,H L
H<L➔
swap pp,H 12 13 14 17 19 21 40 27 30 50

CMP N102 9/10 Spring 2018


Data Structures & Algorithms 1st Year Sheet #8 - Solution

6. What is the suitable algorithm if:


a. the array is nearly sorted Commented [E23]: Insertion sort O(n) in best case
b. the data is too large to fit in memory
Commented [E24]: External sort.

7. What does it mean the sorting algorithm is stable? Give an example when stability really matters. Commented [E25]:
If there are two elements in unsorted array with equal key
Their relative order should be the same in the ordered array
8. What is the best and worst case complexity of all of the above algorithms. Give examples. for the algo to be stable

9. Write the following algorithms to sort an array in ascending order and the first part sorted is that on Example: assume we have array of students where each one
has name,grade
the LEFT. And array should be sorted by grade
a. Selection
b. Insertion Original array
(Ahmed,10), (Ali,5), (Hassan,10)
c. Bubble
d. Shell Now if an algo sorts the array to be
e. Quick (Ali,5) , (Ahmed,10), (Hassan,10)
f. Heap So it is a stable algo.

But if it sorts it to be
10. Repeat question 9 to sort an array in descending order. (Ali,5) , (Hassan,10), (Ahmed,10)
It is not a stable algo.
11. Repeat question 9 but make the first part sorted is that on the RIGHT.

12. Repeat question 10 but make the first part sorted is that on the RIGHT.

CMP N102 10/10 Spring 2018

You might also like