Sorting algorithms/Strand sort: Difference between revisions

Content added Content deleted
(Updated second D entry)
Line 1,284: Line 1,284:
a = self.dup
a = self.dup
result = []
result = []
while a.length > 0
until a.empty?
sublist = [a.shift]
sublist = [a.shift]
a.each_with_index .
a.each_with_index.each_with_object([]) { |(val, idx), remove|
inject([]) do |remove, (val, idx)|
next if val <= sublist.last
if val > sublist[-1]
sublist << val
sublist << val
remove << idx
remove.unshift(idx)
}.reverse_each {|idx| a.delete_at(idx)}
end
remove
result.each_index do |idx|
end .
break if sublist.empty?
result.insert(idx, sublist.shift) if sublist[0] < result[idx]
each {|idx| a.delete_at(idx)}

idx = 0
while idx < result.length and not sublist.empty?
if sublist[0] < result[idx]
result.insert(idx, sublist.shift)
end
idx += 1
end
end
result += sublist if not sublist.empty?
result += sublist
end
end
result
result
end
end

def strandsort!
def strandsort!
self.replace(strandsort)
replace(strandsort)
end
end
end
end