forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request suryanshsk#414 from AnanteshG/10+algos
Added 10 more algos
- Loading branch information
Showing
10 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
Python Snippets/Sorting-Algorithms/Randomized-quick-sort.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |