0% found this document useful (0 votes)
10 views2 pages

Project Notepad

The document contains a Python program that implements linear and binary search algorithms. It prompts the user to input the number of elements and a target value, generates a list of random integers, sorts the list, and then allows the user to choose a search method to find the target. The program measures and displays the execution time for the sorting and searching processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Project Notepad

The document contains a Python program that implements linear and binary search algorithms. It prompts the user to input the number of elements and a target value, generates a list of random integers, sorts the list, and then allows the user to choose a search method to find the target. The program measures and displays the execution time for the sorting and searching processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import time

import random

def linear_search(arr, target):


start_time = time.perf_counter()
for i in range(len(arr)):
if arr[i] == target:
end_time = time.perf_counter()
return i, end_time - start_time
return -1, time.perf_counter() - start_time

def binary_search(arr, target):


start_time = time.perf_counter()
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
end_time = time.perf_counter()
return mid, end_time - start_time
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1, time.perf_counter() - start_time

def main():
n = int(input("Enter the number of elements: "))
data = [random.randint(1, 100) for _ in range(n)]
print("Data:", data)

target = int(input("Enter the target value: "))

start_sort_time = time.perf_counter()
data.sort()
end_sort_time = time.perf_counter()

print("\nSorted Data:", data)


print(f"Time to sort data: {end_sort_time - start_sort_time:.6f} seconds")

print("Choose the search method")


print("1 for Linear Search")
print("2 for Binary Search")
choice = int(input())

linear_time = None
binary_time = None

if choice == 1:
linear_place, linear_time = linear_search(data, target)
if linear_place != -1:
print("Linear Search: Target is found")
print(f"Linear Search Position: {linear_place}")
else:
print("Linear Search: Target not found")
print(f"Linear Search Time: {linear_time:.6f} seconds")
elif choice == 2:
binary_place, binary_time = binary_search(data, target)
if binary_place != -1:
print("Binary Search: Target is found")
print(f"Binary Search Position: {binary_place}")
else:
print("Binary Search: Target not found")
print(f"Binary Search Time: {binary_time:.6f} seconds")
else:
print("Invalid choice!")

if linear_time is not None and binary_time is not None:


print("\nExecution Times:")
print(f"Linear Search Time: {linear_time:.6f} seconds")
print(f"Binary Search Time: {binary_time:.6f} seconds")
elif linear_time is not None:
print("\nExecution Times:")
print(f"Linear Search Time: {linear_time:.6f} seconds")
elif binary_time is not None:
print("\nExecution Times:")
print(f"Binary Search Time: {binary_time:.6f} seconds")

if __name__ == "__main__":
main()

You might also like