0% found this document useful (0 votes)
38 views

Lab No 7

Linear and binary search algorithms were implemented and their time complexities computed. Functions for linear and binary search were created to search lists. Linear search sequentially checks each element while binary search repeatedly selects the middle element until finding the target. Both algorithms were tested on lists of 50 elements, with binary search taking less time due to its divide-and-conquer approach of searching halves rather than linearly checking each element. The lab demonstrated that binary search has a better time complexity than linear search.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab No 7

Linear and binary search algorithms were implemented and their time complexities computed. Functions for linear and binary search were created to search lists. Linear search sequentially checks each element while binary search repeatedly selects the middle element until finding the target. Both algorithms were tested on lists of 50 elements, with binary search taking less time due to its divide-and-conquer approach of searching halves rather than linearly checking each element. The lab demonstrated that binary search has a better time complexity than linear search.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab No.

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

The output of the above code is given below:

For Binary Search:


i. First we make a function by the name binarysearch.
ii. We pass two arguments by the name of array and x
iii. Array is the variable name of list and x is the number which we
have to find in the list
iv. Now we assume the beginning value to zero and ending value is
array -1
v. Now we use the while loop.
vi. Now we use if else conditional statements
vii. Now we make list by the variable name array and pass 50
elements.
viii. Now we sort the list by sort() function.
ix. Now we give some value to x which we have to find.
x. Now we use if else conditional statement to find the result.
xi. Now we use time module to find the time.
xii. The code and the output is shown below

Enter the following code and execute it

The output of the above code is shown below:

Conclusion:
From the above lab we conclude that the executed time of linear search
is greater than the binary search.

You might also like