Skip to content

Commit

Permalink
Merge pull request suryanshsk#414 from AnanteshG/10+algos
Browse files Browse the repository at this point in the history
Added 10 more algos
  • Loading branch information
suryanshsk authored Oct 22, 2024
2 parents 415d36d + 832e97d commit 26b70e3
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Python Snippets/Sorting-Algorithms/Merge-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]

merge_sort(left)
merge_sort(right)

i = j = k = 0

while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1

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

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

# Example usage:
arr = [12, 11, 13, 5, 6, 7]
merge_sort(arr)
print("Merge Sorted Array:", arr)
20 changes: 20 additions & 0 deletions Python Snippets/Sorting-Algorithms/Pigeonhole-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def pigeonhole_sort(arr):
min_val = min(arr)
max_val = max(arr)
size = max_val - min_val + 1
holes = [0] * size

for x in arr:
holes[x - min_val] += 1

i = 0
for count in range(size):
while holes[count] > 0:
arr[i] = count + min_val
i += 1
holes[count] -= 1

# Example usage:
arr = [8, 3, 2, 7, 4, 6, 8]
pigeonhole_sort(arr)
print("Pigeonhole Sorted Array:", arr)
22 changes: 22 additions & 0 deletions Python Snippets/Sorting-Algorithms/Quick-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def partition(arr, low, high):
pivot = arr[high]
i = low - 1

for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1

def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi - 1)
quick_sort(arr, pi + 1, high)

# Example usage:
arr = [10, 7, 8, 9, 1, 5]
quick_sort(arr, 0, len(arr) - 1)
print("Quick Sorted Array:", arr)
33 changes: 33 additions & 0 deletions Python Snippets/Sorting-Algorithms/Radix-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def counting_sort_for_radix(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10

for i in arr:
index = 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(len(arr)):
arr[i] = output[i]

def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
counting_sort_for_radix(arr, exp)
exp *= 10

# Example usage:
arr = [170, 45, 75, 90, 802, 24, 2, 66]
radix_sort(arr)
print("Radix Sorted Array:", arr)
32 changes: 32 additions & 0 deletions Python Snippets/Sorting-Algorithms/Randomized-quick-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import random

# Partition function
def partition(arr, low, high):
pivot = arr[high]
i = low - 1

for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]

arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1

# Randomized partition function
def randomized_partition(arr, low, high):
random_pivot = random.randint(low, high)
arr[random_pivot], arr[high] = arr[high], arr[random_pivot]
return partition(arr, low, high)

# Randomized quick sort
def randomized_quick_sort(arr, low, high):
if low < high:
pi = randomized_partition(arr, low, high)
randomized_quick_sort(arr, low, pi - 1)
randomized_quick_sort(arr, pi + 1, high)

# Example usage:
arr = [10, 7, 8, 9, 1, 5]
randomized_quick_sort(arr, 0, len(arr) - 1)
print("Randomized Quick Sorted Array:", arr)
12 changes: 12 additions & 0 deletions Python Snippets/Sorting-Algorithms/Selection-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]

# Example usage:
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print("Selection Sorted Array:", arr)
18 changes: 18 additions & 0 deletions Python Snippets/Sorting-Algorithms/Shell-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def shell_sort(arr):
n = len(arr)
gap = n // 2

while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2

# Example usage:
arr = [12, 34, 54, 2, 3]
shell_sort(arr)
print("Shell Sorted Array:", arr)
17 changes: 17 additions & 0 deletions Python Snippets/Sorting-Algorithms/Stooge-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def stooge_sort(arr, l, h):
if l >= h:
return

if arr[l] > arr[h]:
arr[l], arr[h] = arr[h], arr[l]

if h - l + 1 > 2:
t = (h - l + 1) // 3
stooge_sort(arr, l, h - t)
stooge_sort(arr, l + t, h)
stooge_sort(arr, l, h - t)

# Example usage:
arr = [2, 4, 5, 3, 1]
stooge_sort(arr, 0, len(arr) - 1)
print("Stooge Sorted Array:", arr)
31 changes: 31 additions & 0 deletions Python Snippets/Sorting-Algorithms/Strand-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def strand_sort(arr):
if len(arr) <= 1:
return arr

result = []
while arr:
sublist = [arr.pop(0)]

for i in range(len(arr)):
if arr[i] > sublist[-1]:
sublist.append(arr[i])
arr.pop(i)

result = merge(result, sublist)

return result

def merge(left, right):
result = []
while left and right:
if left[0] < right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
result.extend(left + right)
return result

# Example usage:
arr = [10, 8, 1, 4, 6, 9]
arr = strand_sort(arr)
print("Strand Sorted Array:", arr)
38 changes: 38 additions & 0 deletions Python Snippets/Sorting-Algorithms/Tree-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def insert(root, key):
if root is None:
return Node(key)
else:
if key < root.val:
root.left = insert(root.left, key)
else:
root.right = insert(root.right, key)
return root

def inorder_traversal(root, result):
if root:
inorder_traversal(root.left, result)
result.append(root.val)
inorder_traversal(root.right, result)

def tree_sort(arr):
if len(arr) == 0:
return []

root = None
for key in arr:
root = insert(root, key)

result = []
inorder_traversal(root, result)
return result

# Example usage:
arr = [5, 4, 7, 2, 11]
arr = tree_sort(arr)
print("Tree Sorted Array:", arr)

0 comments on commit 26b70e3

Please sign in to comment.