Lab No 7
Lab No 7
7
To compute the time complexity of linear and Binary
search.
Objective:
The main Objective of this lab is to learn about the linear and binary
search and to compute their time complexity.
Tasks
In this lab we are assign with three tasks
i. The first one is to make a function for the linear search and to
create a list and find the desired number in the list.
ii. To make a function for the binary search and to find the number
in the list using binary search.
iii. To compute the time complexity of linear and binary search by
importing time module
Theory
Python:
Python is a computer programing language often used to build websites
and software. Python is a general purpose language means it can be
used to create a variety of different programs.
Linear Search:
Linear search is a search that finds an element in the list by searching
the element sequentially until the element is found in the list.
Binary Search:
Binary search is a search that finds the middle element in the list
repeatably until the middle element is matched with a searched
element.
Procedure:
For Linear Search:
i. First we make a function with the name linearsearch.
ii. Now, we pass the three arguments named array,n and x.
iii. Array is the elements in the list, n is number of elements in list,
and x is a search element.
iv. Now we use for loop to search the element.
v. Now, we make a list of 50 elements.
vi. Now we use if else conditional statement to find the desired
element.
vii. Now we use time module to find the time.
viii. The code and the output is shown below:
Enter the following code and execute it
Conclusion:
From the above lab we conclude that the executed time of linear search
is greater than the binary search.