0% found this document useful (0 votes)
19 views3 pages

Big Data EX 3

The document outlines a Python program that implements both linear and binary search algorithms. It includes an algorithm description, code for both search methods, and a demonstration of their functionality using a sample array and target element. The program successfully verifies the search results for both methods.

Uploaded by

giripriya1829
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Big Data EX 3

The document outlines a Python program that implements both linear and binary search algorithms. It includes an algorithm description, code for both search methods, and a demonstration of their functionality using a sample array and target element. The program successfully verifies the search results for both methods.

Uploaded by

giripriya1829
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX NO: PROGRAM TO PERFORM LINEAR AND

DATE : BINARY SEARCH

AIM:

To implement a Python program to perform linear and binary search using Python.
.
ALGORITHM:

1. Start the program.


2. Start with an array and a target element to search.
3. Perform linear search by iterating through the array to find the target.
4. Perform binary search by dividing the array and narrowing the search range.
5. Return the index of the target if found in either search; otherwise, return -1.
6. Print the search results for both methods.

CODE:

def linear_search(arr, target):


for i in range(len(arr)):
if arr[i] == target:
return i
return -1

def binary_search(arr, target):


left = 0
right = len(arr) - 1

while left <= right:


mid = (left + right) // 2

if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1

if _name_ == "_main_":
arr = [10, 20, 30, 40, 50, 60, 70, 80, 90]
target = 50
linear_result = linear_search(arr, target)
if linear_result != -1:
print(f"Linear Search: Element {target} found at index {linear_result}.")
else:
print("Linear Search: Element not found.")

binary_result = binary_search(arr, target)


if binary_result != -1:
print(f"Binary Search: Element {target} found at index {binary_result}.")
else:
print("Binary Search: Element not found.")

OUTPUT:

RESULT:

Thus the program to perform linear and binary search is implemented and the output
is verified successfully.

You might also like