Skip to content

Commit

Permalink
Merge pull request #389 from Shreya-0309/main
Browse files Browse the repository at this point in the history
Added Additional Sorting Algorithms
  • Loading branch information
larymak committed Jul 1, 2024
2 parents 37c6353 + 1fcf2f0 commit 2288b34
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Data Structures and Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- Bogo Sort

- Cocktail Sort

- Counting Sort

- Linear search (for numbers)
Expand Down Expand Up @@ -36,6 +38,8 @@

- Recursive Binary Search

- Shell Sort

- Selection Sort

- Binary Tree traversal
Expand Down
27 changes: 27 additions & 0 deletions Data Structures and Algorithms/Sorting Algorithms/cocktail_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys

def cocktail_sort(arr):
n = len(arr)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False

for i in range(start, end):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
if not swapped:
break
swapped = False
end -= 1

for i in range(end - 1, start - 1, -1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
start += 1
return arr

print(cocktail_sort([5, 8, 1, 4, 7, 9, 6, 3, 2]))
18 changes: 18 additions & 0 deletions Data Structures and Algorithms/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 # gap size

while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
# Perform insertion sort for the elements separated by the gap
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2

return arr

print(shell_sort([5, 8, 1, 4, 7, 9, 6, 3, 2]))

0 comments on commit 2288b34

Please sign in to comment.