Docsity

Binary Search and Heap Algorithms: Recursive Binary Search and Complexity Analysis, Exercises of Aeronautical Engineering

Toggle sidebar
bg1
Home Work 10
The problems in this problem set cover lectures C11 and C12
1.
a. Define a recursive binary search algorithm.
If lb > ub
Return -1
else
Mid := (lb+ub)/2
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.
46. function Binary_Search (My_Search_Array : My_Array; Lb : Integer; Ub: Integer; Element : Integer)
return Integer is
47. mid : integer;
48. begin
49. if (Lb> Ub) then
50. return -1;
51. else
52. Mid := (Ub+Lb)/2;
53. if My_Search_Array(Mid) = Element then
54. return(Mid);
55. elsif My_Search_Array(Mid) < Element then
56. return (Binary_Search(My_Search_Array, Mid+1, Ub, Element));
57. else
58. return (Binary_Search(My_Search_Array, Lb, Mid-1, Element));
59. end if;
60. end if;
61.
62. end Binary_Search;
63. end Recursive_Binary_Search;
docsity.com
pf3
pf4

Partial preview of the text

Download Binary Search and Heap Algorithms: Recursive Binary Search and Complexity Analysis and more Exercises Aeronautical Engineering in PDF only on Docsity!

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.

  1. function Binary_Search (My_Search_Array : My_Array; Lb : Integer; Ub: Integer; Element : Integer) return Integer is
    1. mid : integer;
    2. begin
    3. if (Lb> Ub) then
    4. return -1;
    5. else
    6. Mid := (Ub+Lb)/2;
    7. if My_Search_Array(Mid) = Element then
    8. return(Mid);
    9. elsif My_Search_Array(Mid) < Element then
    10. return (Binary_Search(My_Search_Array, Mid+1, Ub, Element));
    11. else
    12. return (Binary_Search(My_Search_Array, Lb, Mid-1, Element));
    13. end if;
    14. end if;
    15. end Binary_Search;
    16. end Recursive_Binary_Search;

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

T(n) =

log (^) b a

O?? a? b

k

n

k k

O? n log bn? a? b

k^ 1 = 2^0 , hence the second term is used,

??? b

k

T(n) =

  1. What is the Big-O complexity of : a. Heapify function

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)