3 ms·
I find it funny that the 31 lines of code in the example can be easily reduced to 8: def merge(a, b) c = [] c << (a[0] > b[0] ? b.shift : a.shift) un
by carpal 19y ago
I find it funny that the 31 lines of code in the example can be easily reduced to 8:
def merge(a, b)
c = []
c << (a[0] > b[0] ? b.shift : a.shift) until a.empty? || b.empty?
return c+a+b
end
def mergesort(a)
a.size <= 1 ? a : merge(mergesort(a.slice!(0,a.size/2)),mergesort(a))
end
Language Power > Language Speed
- mrtron 19y agoI also thought what was humorous was if you were using python, you wouldn't use a python sorting algorithm for numbers. I realize it is just an example, but if you want something realistic, use the built in sort which is nicely optimized, or write your own method in C if it is non-trivial. >def realsort(): t1 = datetime.datetime.now() for x in range(3000): numbers[:].sort() t2 = datetime.datetime.now() print t2-t1 >realsort() >0:00:00.128081