Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -8,7 +8,7 @@ class Array
swapped = true
end
end
break if not swapped
break unless swapped
swapped = false
(length - 2).downto(0) do |i|
@ -21,6 +21,3 @@ class Array
self
end
end
ary = [7,6,5,9,8,4,3,1,2,0]
ary.cocktailsort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

View file

@ -0,0 +1,17 @@
class Array
def cocktailsort!
start, finish, way = 0, size-1, 1
loop do
swapped = false
start.step(finish-way, way) do |i|
if (self[i] <=> self[i + way]) == way
self[i], self[i + way] = self[i + way], self[i]
swapped = i
end
end
break unless swapped
start, finish, way = swapped, start, -way
end
self
end
end

View file

@ -0,0 +1,4 @@
ary = [7,6,5,9,8,4,3,1,2,0]
p ary.cocktailsort!
ary = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]
p ary.cocktailsort!