LAB_3
LAB_3
LAB_3
import time
import matplotlib.pyplot as plt
import numpy as np
start_time = time.perf_counter()
binary_search(arr, target)
end_time = time.perf_counter()
data.append((n, execution_time))
print(f"{n}\t {execution_time:.6f}")
# Generate plot
n_values, time_values = zip(*data)
plt.figure(figsize=(7, 5))
# Plot for n vs execution time
plt.plot(n_values, time_values, label='Execution Time')
plt.title('n vs Execution Time (Binary Search)')
plt.xlabel('n')
plt.ylabel('Execution Time (seconds)')
plt.legend()
plt.show()
Output:
Time Complexity of Binary Search using Substitution Method:
{()
1 ,∧n=1
T (n)=
T
n
2
T
+c ,∧n>1 2
n
() n
that the a problem of ¿is divided into problem of ¿( ¿ ) each .
2
c is constant ti signifiesme needed for comparison and computing mid can be taken as 1
{()
1,∧n=1
T (n)=
T
n
2
+1 ,∧n>1
T ( n )=T
n
2 ()
+1Using substitution method T ( n )= T
n
4 (() )
+1 +1
( 8 )
(
T ( n )= T
n
)+ 1 +1T ( n )=T ( )+32 =n ,thus k =lo g n
n
8
k
T (n)=k + T
( )
n
2
k ()
ma x value of k can be lo g nT (n)=lo g n+ T
n
n
T (n)=logn+ T ( 1 )T (n)=lo g n