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

@ -0,0 +1,19 @@
split = { list ->
list.collate((list.size()+1)/2 as int)
}
merge = { left, right, headBuffer=[] ->
if(left.size() == 0) headBuffer+right
else if(right.size() == 0) headBuffer+left
else if(left.head() <= right.head()) merge.trampoline(left.tail(), right, headBuffer+left.head())
else merge.trampoline(right.tail(), left, headBuffer+right.head())
}.trampoline()
mergesort = { List list ->
if(list.size() < 2) list
else merge(split(list).collect {mergesort it})
}
assert mergesort((500..1)) == (1..500)
assert mergesort([5,4,6,3,1,2]) == [1,2,3,4,5,6]
assert mergesort([3,3,1,4,6,78,9,1,3,5]) == [1,1,3,3,3,4,5,6,9,78]

View file

@ -0,0 +1,4 @@
split = { list, left=[], right=[] ->
if(list.size() <2) [list+left, right]
else split.trampoline(list.tail().tail(), [list.head()]+left,[list.tail().head()]+right)
}.trampoline()

View file

@ -1,7 +1,7 @@
merge [] ys = ys
merge xs [] = xs
merge xs@(x:xs') ys@(y:ys') | x <= y = x : merge xs' ys
| otherwise = y : merge xs ys'
merge [] ys = ys
merge xs [] = xs
merge xs@(x:xt) ys@(y:yt) | x <= y = x : merge xt ys
| otherwise = y : merge xs yt
split (x:y:zs) = let (xs,ys) = split zs in (x:xs,y:ys)
split [x] = ([x],[])

View file

@ -1,34 +1,26 @@
def merge_sort(m)
if m.length <= 1
return m
end
return m if m.length <= 1
middle = m.length / 2
left = m[0,middle]
right = m[middle..-1]
middle = m.length / 2
left = m[0,middle]
right = m[middle..-1]
left = merge_sort(left)
right = merge_sort(right)
merge(left, right)
left = merge_sort(left)
right = merge_sort(right)
merge(left, right)
end
def merge(left, right)
result = []
until left.empty? || right.empty?
# change the direction of this comparison to change the direction of the sort
if left.first <= right.first
result << left.shift
else
result << right.shift
end
result = []
until left.empty? || right.empty?
if left.first <= right.first
result << left.shift
else
result << right.shift
end
unless left.empty?
result += left
end
unless right.empty?
result += right
end
result
end
result + left + right
end
ary = [7,6,5,9,8,4,3,1,2,0]
p merge_sort(ary) # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

View file

@ -1,36 +1,32 @@
class Array
def mergesort(&comparitor)
if length <= 1
self
else
unless comparitor
comparitor = lambda {|a, b| a <=> b}
end
middle = length / 2
left = self[0, middle].mergesort(&comparitor)
right = self[middle..-1].mergesort(&comparitor)
merge(left, right, comparitor)
end
return self if length <= 1
comparitor ||= lambda {|a, b| a <=> b}
middle = length / 2
left = self[0, middle].mergesort(&comparitor)
right = self[middle..-1].mergesort(&comparitor)
merge(left, right, comparitor)
end
protected
private
def merge(left, right, comparitor)
if left.empty?
right
elsif right.empty?
left
elsif comparitor.call(left.first, right.first) <= 0
[left.first] + merge(left[1..-1], right, comparitor)
else
[right.first] + merge(left, right[1..-1], comparitor)
result = []
until left.empty? || right.empty?
# change the direction of this comparison to change the direction of the sort
if comparitor[left.first, right.first] <= 0
result << left.shift
else
result << right.shift
end
end
result + left + right
end
end
ary = [7,6,5,9,8,4,3,1,2,0]
ary.mergesort # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
ary.mergesort {|a, b| b <=> a} # => [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
p ary.mergesort # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
p ary.mergesort {|a, b| b <=> a} # => [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
ary = [["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]
ary.mergesort {|a, b| a[1] <=> b[1]}
p ary.mergesort {|a, b| a[1] <=> b[1]}
# => [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]