Docsity

Binary Search Algorithm-Basic Unified Engineering-Assignment Solution, Exercises of 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
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Binary Search Algorithm-Basic Unified Engineering-Assignment Solution and more Exercises 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)

� � � � � �� � � �

� �

� � �� � � � � � � �� � � �

Unified Engineering II Spring 2004

Problem S10 (Signals and Systems) Solution

  1. Because the numerator is the same order as the denominator, the partial fraction expansion will have a constant term:

3 s^2 + 3s − 10 G(s) = (^2) s − 4 3 s^2 + 3s − 10 = (s − 2)(s + 2) b c = a + + s − 2 s + 2

To find a, b, and c, use coverup method:

a = G(s)| (^) s=∞= 3 3 s^2 + 3s − 10 b = s + 2

s= 3 s^2 + 3s − 10 c = = 1 s − (^2) s=− 2

So 2 1 G(s) = 3 + s − 2

s + 2

, Re[s] > 2

We can take the inverse LT by simple pattern matching. The result is that

g(t) = 3δ(t) + 2 e^2 t^ + e−^2 t^ σ(t)

6 s^2 + 26s + 26 G(s) = (s + 1)(s + 2)(s + 3) a b c = + + s + 1 s + 2 s + 3 Using partial fraction expansions,

6 s^2 + 26s + 26 (s + 2)(s + 3)

a = = 3 s=− 1 6 s^2 + 26s + 26 b = = 2 (s + 1)(s + 3) (^) s=− 2 6 s^2 + 26s + 26 c = = 1 (s + 1)(s + 2) (^) s=− 3

� �

�� � � � � � �� � � � � � � �� � � �

So 4 s^3 + 11s^2 + 5s + 2 a 2 c 4 G(s) = = + + + s^2 (s + 1)^2 s s^2 s + 1 (s + 1)^2 To find a and c, pick two values of s, say, s = 1 and s = 2. Then 4 + 11 + 5 + 2 a 2 c 4 G(1) = = + + + 12 (1 + 1)^2 1 12 1 + 1 (1 + 1)^2 4 23 + 11 22 + 5 2 + 2 a 2 c 4 G(2) =

22 (2 + 1)^2 2 22 2 + 1 (2 + 1)^2

Simplifying, we have that c 5 a + = 2 2 a c 3

  • = 2 3 2 Solving for a and c, we have that

a = 1 c = 3

So 1 2 3 4 G(s) = + + + s s^2 s + 1 (s + 1)^2 and g(t) = 1 + 2t + 3e−t^ + 4te−t^ σ(t)

  1. G(s) can be expanded as

s^3 + 3s^2 + 9s + 12 G(s) = (s^2 + 4) (s^2 + 9) s^3 + 3s^2 + 9s + 12 = (s + 2j)(s − 2 j)(s + 3j)(s − 3 j) a b c d = + + + s + 2j s − 2 j s + 3j s − 3 j The coefficients can be found by the coverup method: s^3 + 3s^2 + 9s + 12 (s − 2 j)(s + 3j)(s − 3 j)

a = = 0. 5 s=− 2 j s^3 + 3s^2 + 9s + 12 b = = 0. 5 (s + 2j)(s + 3j)(s − 3 j) (^) s=+2j s^3 + 3s^2 + 9s + 12 c = = 0. 5 j (s + 2j)(s − 2 j)(s − 3 j) s^3 + 3s^2 + 9s + 12 d = (s + 2j)(s − 2 j)(s + 3j)

s=− 3 j

s=+3j

= − 0. 5 j

� �

Therefore

  1. 5 0. 5 G(s) = + +

  2. 5 j

− 0. 5 j , Re[s] > 0 s + 2j s − 2 j s + 3j s − 3 j

and the inverse LT is

g(t) = 0. 5 e−^2 jt^ + e^2 jt^ + je−^3 jt^ − je^3 jt σ(t)

This can be expanded using Euler’s formula, which states that

eajt^ = cos at + j sin at

Applying Euler’s formula yields

g(t) = (cos 2t + sin 2t) σ(t)

(^00 100 200 300 400 500 )

20

40

60

80

100

120

140

160

180

Impulse response, g(t)

Time, t (sec)

  1. From the problem statement,

H(s) k G¯(s)

R(s) 1 + k G¯(s) 1 k (^2) s(s + 2ζωns + ω^2 n) = (^1) 1 + k s(s^2 + 2ζωns + ω^2 n) k = s^3 + 2ζωns^2 + ω^2 n^ s + k

So the poles of the system are the roots of the denominator polynomial,

φ(s) = s^3 + 2ζωns^2 + ω^2 s + k = 0 n

The roots can be found using Matlab, a programmable calculator, etc. The plot of the roots (the “root locus”) is shown below. Note that the oscillatory poles go unstable at a gain of only k = 0.000118.

-0.5-0.5 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.

-0.

-0.

-0.

-0.

0

Real part of s

Imaginary part of s

  1. The roots locus for negative gains can be plotted in a similar way, as below. Note that the real pole is unstable for all negative k.

-0.5-0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.

-0.

-0.

-0.

-0.

0

Real part of s

Imaginary part of s

the integral converges only for Re[s] < a. To find the integral, integrate by parts: � (^0) G(s) = te(a−s)t^ dt −∞ 1 a − s

e(a−s)t

0 1 �^0

= t − e(a−s)t^ dt a − s (^) −∞ � (^0) −∞ (^1) (a−s)t = 0 − e dt a − s (^) −∞ 1 0 = − e (a − s)^2

(a−s)t −∞ 1 = − Re[s] < a (s − a)^2

g(t) = cos(ω t 0 t)^ e −a| |, for all t

The LT is given by ∞ cos(ω^ t 0 t)^ e G(s) = −a| |e−st^ dt −∞ For the LT to converge, the integrand must go to zero as t goes to −∞ and ∞. Therefore, the integral converges only for −a < Re[s] < a. The integral is given by ∞ G(s) = cos(ω 0 t) e−a|t|e−st^ dt −∞ � (^0) ∞ at (^) e−st (^) dt cos(ω 0 t)^ e cos(ω −at^ e−st^ dt = 0 t) e + −∞ 0 Expanding the cosine term as

ejω^0 t^ + e−jω^0 t cos(ω 0 t) = 2 yields � (^0) ejω^0 t^ + e−jω^0 t^ at ∞ejω^0 t^ + e−jω^0 t G(s) = e e−st^ dt + e−at^ e−st^ dt −∞^2 � (^0) e(jω^0 +a−s)t^ + e(−jω^0 +a−s)t dt +

∞e(jω 0 −a−s)t (^) + e(−jω 0 −a−s)t = dt −∞^2 0 1 1 1 1 1 = + 2 jω 0 + a − s −jω 0 + a − s

jω 0 − a − s

−jω 0 − a − s s + a =

s − a , −a < Re[s] < a s^2 + 2as + a^2 + ω^2

0 s^2 −^2 as^ +^ a^2 +^ ω^20