Skip to content

Instantly share code, notes, and snippets.

@philangist
Created August 4, 2013 00:17
Show Gist options
  • Save philangist/6148511 to your computer and use it in GitHub Desktop.
Save philangist/6148511 to your computer and use it in GitHub Desktop.
I saw the cocktail shaker sort in the [15 Sorting Algorithms in 6 Minutes](https://www.youtube.com/watch?v=kPRA0W1kECg) video and wanted to implement it.
import random
def cocktail_shaker_sort(array):
smallest_array=[]
largest_array = []
while len(array) > 0:
smallest_element = min(array)
smallest_element_index = array.index(smallest_element)
smallest_array.append(smallest_element)
array.pop(smallest_element_index)
largest_element = max(array)
largest_element_index = array.index(largest_element)
largest_array.insert(0, largest_element)
array.pop(largest_element_index)
return smallest_array + largest_array
array = range(50)
random.shuffle(array)
print 'unsorted array: ', array, '\n\nsorted array: ', cocktail_shaker_sort(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment