Skip to content
Closed
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
39 changes: 39 additions & 0 deletions src/python/Cocktail_Shaker_Sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def cocktail_shaker_sort(arr):
n = len(arr)
swapped = True
start = 0
end = n - 1

while swapped:
# reset the swapped flag on entering the loop
swapped = False

# Traverse the list from left to right
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 no elements were swapped, the list is already sorted
if not swapped:
break

# Reset the swapped flag for the next stage
swapped = False

# Move the end point back by one
end -= 1

# Traverse the list from right to left
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

# Move the start point forward by one
start += 1

# Example usage
arr = [5, 1, 4, 2, 8, 0, 2]
cocktail_shaker_sort(arr)
print("Sorted array:", arr)
Loading