Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 46 additions & 70 deletions search_algos.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,82 @@
from complexity import time_and_space_profiler
from tqdm import tqdm , trange
from tqdm import tqdm
import numpy as np
import pandas as pd



# Inialization logic
# TODO: 30 tests should be implemented for
# 1000;10000;100000;1000000;10000000

# Initialization logic
# Generating 30 tests for arrays of lengths between 1000 and 10,000,000 with 5 tests per length
np.random.seed(42)

tests = []

#lenghts = [1000,10000,100000,1000000]

lenghts = np.array(range(1000,1000000,20000))

lengths = np.array(range(1000, 10_000_000, 200_000))
tests_per_length = 5

for length in lenghts:
for length in lengths:
for _ in range(tests_per_length):

val = np.sort(np.random.randint(1,4*length,size=length))

target = np.random.randint(1,4*length)

test = (length, val, target)

tests.append(test)

# Generate a sorted array and a random target
val = np.sort(np.random.randint(1, 4 * length, size=length))
target = np.random.randint(1, 4 * length)
tests.append((length, val, target))

# Function definitions

@time_and_space_profiler
def sequantial_search(val,target):
def sequential_search(val, target):
"""Performs a simple sequential search."""
for i in range(len(val)):
if target == val[i]: # comparision
return i+1

if target == val[i]: # Comparison
return i + 1
return len(val)

#@time_and_space_profiler
#def quadratic_sequantial_search(val,target):
# for i in range(len(val)):
# for j in range(len(val)):
# if target == val[i]: # comparision
# return i+1
#
# return len(val)*len(val)

@time_and_space_profiler
def advanced_sequantial_search(val,target):
def advanced_sequential_search(val, target):
"""Optimized sequential search that stops early when the target is less than the current element."""
comparison = 0
for i in range(len(val)):
comparison+=2
if target == val[i]: # comparision
comparison-=1
comparison += 2
if target == val[i]: # Match found
comparison -= 1
break
# add the part to stop searching when target small to the element
elif target < val[i]:
elif target < val[i]: # No need to search further
break

return comparison

@time_and_space_profiler
def binary_search(val,target):
def binary_search(val, target):
"""Performs binary search."""
start = 0
end = len(val)-1
end = len(val) - 1
comparison = 1

while start < end:
half = (end + start)//2
comparison+=2
if target == val[half]:
comparison-=1
while start <= end:
half = (end + start) // 2
comparison += 2
if target == val[half]: # Match found
comparison -= 1
break
elif target < val[half]:
end = half -1
else:
start = half + 1
comparison+=1
return comparison

funcs = [sequantial_search, advanced_sequantial_search,binary_search]
elif target < val[half]: # Search left
end = half - 1
else: # Search right
start = half + 1
comparison += 1
return comparison

# List of functions to test
funcs = [sequential_search, advanced_sequential_search, binary_search]

# Run tests and collect results
results = []
for i , (length, val ,target) in tqdm(enumerate(tests),ncols=len(tests)):

for i, (length, val, target) in tqdm(enumerate(tests), total=len(tests), desc="Testing progress"):
for func in funcs:
func_name, comparison, T, S = func(val, target)
results.append((i, func_name, length, comparison, T, S))

func_name,comparison,T, S = func(val,target)

results.append((i,func_name,length,comparison,T,S))


# Printing results
#print(results)

df = pd.DataFrame(results, columns=['id_test','function_name','array_length','comparison','time','space'])
# Convert results to a DataFrame
df = pd.DataFrame(results, columns=['id_test', 'function_name', 'array_length', 'comparison', 'time', 'space'])

# Output results
print(df)

df.to_csv('results.csv', index=False)
# Save results to a CSV file
df.to_csv('results.csv', index=False)