Skip to content

Commit

Permalink
Add searching_sorting.py and test_searching_sorting.py
Browse files Browse the repository at this point in the history
Add searching and sorting with test cases
  • Loading branch information
simrantyagi01 committed Nov 6, 2024
1 parent 71ec897 commit 65a563b
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Tests/algorithms/test_searching_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest
from searching_sorting import linear_search, binary_search, jump_search, exponential_search
from searching_sorting import bubble_sort, selection_sort, insertion_sort, merge_sort, quick_sort, heap_sort, counting_sort, radix_sort

class TestAlgorithms(unittest.TestCase):

# Searching Algorithms Test Cases

def test_linear_search(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(linear_search(arr, 9), 2)
self.assertEqual(linear_search(arr, 10), -1)

def test_binary_search(self):
arr = [1, 2, 5, 5, 6, 9]
self.assertEqual(binary_search(arr, 9), 5)
self.assertEqual(binary_search(arr, 10), -1)

def test_jump_search(self):
arr = [1, 2, 5, 5, 6, 9]
self.assertEqual(jump_search(arr, 9), 5)
self.assertEqual(jump_search(arr, 10), -1)

def test_exponential_search(self):
arr = [1, 2, 5, 5, 6, 9]
self.assertEqual(exponential_search(arr, 9), 5)
self.assertEqual(exponential_search(arr, 10), -1)

# Sorting Algorithms Test Cases

def test_bubble_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(bubble_sort(arr), [1, 2, 5, 5, 6, 9])

def test_selection_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(selection_sort(arr), [1, 2, 5, 5, 6, 9])

def test_insertion_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(insertion_sort(arr), [1, 2, 5, 5, 6, 9])

def test_merge_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(merge_sort(arr), [1, 2, 5, 5, 6, 9])

def test_quick_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(quick_sort(arr), [1, 2, 5, 5, 6, 9])

def test_heap_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(heap_sort(arr), [1, 2, 5, 5, 6, 9])

def test_counting_sort(self):
arr = [5, 2, 9, 1, 5, 6]
self.assertEqual(counting_sort(arr), [1, 2, 5, 5, 6, 9])

def test_radix_sort(self):
arr = [170, 45, 75, 90, 802, 24, 2, 66]
self.assertEqual(radix_sort(arr), [2, 24, 45, 66, 75, 90, 170, 802])


if __name__ == "__main__":
unittest.main()
205 changes: 205 additions & 0 deletions pysnippets/Searching_Sorting/searching_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
# Searching Algorithms

def linear_search(arr, target):
"""Linear Search: Search for a target in a list"""
for i, value in enumerate(arr):
if value == target:
return i
return -1


def binary_search(arr, target):
"""Binary Search: Search for a target in a sorted list"""
left, right = 0, 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


def jump_search(arr, target):
"""Jump Search: Search for a target in a sorted list"""
n = len(arr)
step = int(n ** 0.5)
prev, curr = 0, 0
while curr < n and arr[curr] < target:
prev = curr
curr = min(curr + step, n - 1)
for i in range(prev, curr + 1):
if arr[i] == target:
return i
return -1


def exponential_search(arr, target):
"""Exponential Search: Search for a target in a sorted list"""
if arr[0] == target:
return 0
i = 1
while i < len(arr) and arr[i] <= target:
i *= 2
return binary_search(arr[i // 2:min(i, len(arr))], target)


# Sorting Algorithms

def bubble_sort(arr):
"""Bubble Sort: Sort an array using bubble sort"""
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr


def selection_sort(arr):
"""Selection Sort: Sort an array using selection sort"""
n = len(arr)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr


def insertion_sort(arr):
"""Insertion Sort: Sort an array using insertion sort"""
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr


def merge_sort(arr):
"""Merge Sort: Sort an array using merge sort"""
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]

merge_sort(left_half)
merge_sort(right_half)

i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1

while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1

while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
return arr


def quick_sort(arr):
"""Quick Sort: Sort an array using quick sort"""
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)


def heap_sort(arr):
"""Heap Sort: Sort an array using heap sort"""
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)

n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)

for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)

return arr


def counting_sort(arr):
"""Counting Sort: Sort an array using counting sort"""
if not arr:
return arr
max_val = max(arr)
min_val = min(arr)
range_of_elements = max_val - min_val + 1
count = [0] * range_of_elements
output = [0] * len(arr)

for num in arr:
count[num - min_val] += 1

for i in range(1, range_of_elements):
count[i] += count[i - 1]

for num in reversed(arr):
output[count[num - min_val] - 1] = num
count[num - min_val] -= 1

return output


def radix_sort(arr):
"""Radix Sort: Sort an array using radix sort"""
def counting_sort_for_radix(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

for i in range(n):
index = arr[i] // exp
count[index % 10] += 1

for i in range(1, 10):
count[i] += count[i - 1]

i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1

for i in range(n):
arr[i] = output[i]

max_num = max(arr)
exp = 1
while max_num // exp > 0:
counting_sort_for_radix(arr, exp)
exp *= 10
return arr

0 comments on commit 65a563b

Please sign in to comment.