


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A recursive binary search algorithm in ada95, along with the analysis of its computation time and big-o complexity. Additionally, it includes the heapify and build-heap functions, and the analysis of their big-o complexity and heap sort algorithm.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Home Work 10
The problems in this problem set cover lectures C1 1 and C1 2
a. Define a recursive binary search algorithm.
If lb > ub Return - else Mid := (lb+ub)/ If Array(Mid) = element Return Mid Elsif Array(Mid) < Element Return Binary_Search(Array, mid+1, ub, Element) Else Return Binary_Search(Array, lb, mid-1, Element) End if End if
b. Implement your algorithm as an Ada95 program.
c. What is the recurrence equation that represents the computation time of your algorithm?
Recursive Binary Search Cost if (Lb> Ub) then c return -1; c else c Mid := (Ub+Lb)/2; c if My_Search_Array(Mid) = Element then c return(Mid); c elsif My_Search_Array(Mid) < Element then c return (Binary_Search(My_Search_Array, Mid+1, Ub, Element)); T(n/2) else c return (Binary_Search(My_Search_Array, Lb, Mid-1, Element)); T(n/2) end if; c end if; c
In this case, only one of the recursive calls is made, hence only one of the T(n/2) terms is included in the final cost computation.
Therefore T(n) = (c1+c2+c3+c4+c5+c6+c7+c8+c9+c10) + T(n/2) = T(n/2) + C
d. What is the Big-O complexity of your algorithm? Show all the steps in the computation based on your algorithm.
T(n) = T(n/2) + C ¥ T(n) = aT(n/b) + cnk , where a,c > 0 and b > 1
log (^) b a
k
k k
k^ 1 = 2^0 , hence the second term is used,
k
T(n) =
A heap is an array that satisfies the heap properties i.e., A(i) ≤ A(2i) and A(i) ≤ A(2i+1).
Simplifying => T(n) = O(n log(n) )
c. Heap_Sort
Heap Sort Cost t(n)
Build_Heap(Heap_Array, Size); O(nlogn)) for I in reverse 2.. size loop n Swap(Heap_Array, 1, I); c1(n-1) Heap_Size:= Heap_Size -1; c2(n-1) Heapify(Heap_Array, 1); O(log n)(n-1)
T(n) = 2 O(nlogn) + (c1+c2+1)n - O(log n) + = 2 O(nlog n) - O(log n) + c‘n
Simplifying, => T(n) = O(nlogn)