Assignment 1 Aman Mansoori
Assignment 1 Aman Mansoori
Implement searching algo. and prepare analysis report keeping focus on Space and Time
Complexity.
1. LINEAR SEARCH
Implement searching algo. and prepare analysis report keeping focus on Space and
Time Complexity.
Python Implementation:
def linear_search():
found = False
for i in range(size):
if arr[i] == target:
print(f"Element found at index {i}")
found = True
break
if not found:
print("Element not found in the array.")
linear_search()
Output:
ANALYSIS REPORT
Time Complexity:
Space Complexity:
2. BINARY SEARCH
Binary Search works on sorted arrays. It repeatedly divides the search interval in half.
Python Implementation:
def binary_search():
left = 0
right = size - 1
found = False
if arr[mid] == target:
print(f"Element found at index {mid}")
found = True
break
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
if not found:
print("Element not found in the array.")
binary_search()
Output:
ANALYSIS REPORT
Time Complexity:
Space Complexity:
• Iterative: O(1)
• Recursive: O(log n) due to call stack.