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

Assignment 1 Aman Mansoori

The document outlines the implementation and analysis of two searching algorithms: Linear Search and Binary Search. Linear Search has a time complexity of O(1) in the best case and O(n) in the worst case, with a space complexity of O(1). Binary Search, applicable to sorted arrays, has a best case time complexity of O(1) and O(log n) for average and worst cases, with iterative space complexity of O(1) and recursive space complexity of O(log n).

Uploaded by

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

Assignment 1 Aman Mansoori

The document outlines the implementation and analysis of two searching algorithms: Linear Search and Binary Search. Linear Search has a time complexity of O(1) in the best case and O(n) in the worst case, with a space complexity of O(1). Binary Search, applicable to sorted arrays, has a best case time complexity of O(1) and O(log n) for average and worst cases, with iterative space complexity of O(1) and recursive space complexity of O(log n).

Uploaded by

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

ASSIGNMENT 1

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():

size = int(input("Enter the number of elements in the


array: "))
arr = []
for i in range(size):
num = int(input(f"Enter element {i + 1}: "))
arr.append(num)

target = int(input("Enter the element to search for:


"))

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:

• Best Case: O(1) → Target is at the beginning.


• Average Case: O(n)

• Worst Case: O(n) → Target is at the end or not present.

Space Complexity:

• O(1) → Only a constant amount of space is used (no extra storage).

2. BINARY SEARCH

Binary Search works on sorted arrays. It repeatedly divides the search interval in half.
Python Implementation:
def binary_search():

size = int(input("Enter the number of elements in the


sorted array: "))
arr = []
for i in range(size):
num = int(input(f"Enter element {i + 1} (in
ascending order): "))
arr.append(num)

target = int(input("Enter the element to search for:


"))

left = 0
right = size - 1
found = False

while left <= right:


mid = (left + right) // 2

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:

• Best Case: O(1) → Target is at the middle.


• Average Case: O(log n)
• Worst Case: O(log n)

Space Complexity:

• Iterative: O(1)
• Recursive: O(log n) due to call stack.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy