Created
January 27, 2013 05:22
-
-
Save mclaughj/4646566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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