Skip to content

Instantly share code, notes, and snippets.

@mclaughj
Created January 27, 2013 05:22
Show Gist options
  • Save mclaughj/4646566 to your computer and use it in GitHub Desktop.
Save mclaughj/4646566 to your computer and use it in GitHub Desktop.
def cocktailsort(array)
arr = Array.new(array)
swapped = true
while swapped do
swapped = false
for i in 0.upto(arr.length-2) do
if arr[i] > arr[i+1] then
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
end
end
!swapped ? break : nil
swapped = false
for i in (arr.length-2).downto(0) do
if arr[i] > arr[i+1] then
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
end
end
end
arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment