Professional Documents
Culture Documents
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
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.
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.
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
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
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
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
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
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
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
Solution:
3. Repeat question 1 but with make the first part sorted is that on the RIGHT.
Solution:
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
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
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.
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
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
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.
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
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
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.