Professional Documents
Culture Documents
ANS;
mid
= (beg + end) / 2
= (0 + 6) / 2
=3
It continues searching until either the element 30 is found or all the elements are searched.
Now,
Now, it stops the comparison and returns index 0 at which element 30 is present.
ANS;
mid
= (beg + end) / 2
= (0 + 6) / 2
=3
Since a[mid] = 51< 91, so we take beg = mid + 1 = 3 + 1 = 4 whereas end remains unchanged.
mid
= (beg + end) / 2
= (4 + 6) / 2
=5
Since a[mid] = 88< 91, so we take beg = mid + 1 = 5 + 1 = 6 whereas end remains unchanged.
mid
= (beg + end) / 2
= (6+ 6) / 2
=6
a) Discuss the time complexity of Linear search algorithm when target is not found.
ANS;
In every iteration, compare the target value with the current value of the array.
If the values match, return the current index of the array.
If the values do not match, move on to the next array element.
If no match is found, return -1.
b) Discuss the time complexity of Binary search algorithm when target is not found.
ANS;
If the target value is equal to the middle element of the array, then return the index of the
middle element.
If not, then compare the middle element with the target value,
When a match is found, return the index of the element matched.
If no match is found, then return -1