2016-12-04 15:00:20 +00:00
|
|
|
def cocktail_shaker_sort(unsorted):
|
|
|
|
"""
|
|
|
|
Pure implementation of the cocktail shaker sort algorithm in Python.
|
|
|
|
"""
|
|
|
|
for i in range(len(unsorted)-1, 0, -1):
|
|
|
|
swapped = False
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
for j in range(i, 0, -1):
|
|
|
|
if unsorted[j] < unsorted[j-1]:
|
|
|
|
unsorted[j], unsorted[j-1] = unsorted[j-1], unsorted[j]
|
|
|
|
swapped = True
|
|
|
|
|
|
|
|
for j in range(i):
|
|
|
|
if unsorted[j] > unsorted[j+1]:
|
|
|
|
unsorted[j], unsorted[j+1] = unsorted[j+1], unsorted[j]
|
|
|
|
swapped = True
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
if not swapped:
|
|
|
|
return unsorted
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2016-12-04 15:00:20 +00:00
|
|
|
if __name__ == '__main__':
|
2019-08-19 13:37:49 +00:00
|
|
|
user_input = input('Enter numbers separated by a comma:\n').strip()
|
2016-12-04 15:00:20 +00:00
|
|
|
unsorted = [int(item) for item in user_input.split(',')]
|
|
|
|
cocktail_shaker_sort(unsorted)
|
2017-11-25 09:23:50 +00:00
|
|
|
print(unsorted)
|