r/dailyprogrammer • • May 16 '12

[5/16/2012] Challenge #53 [easy]

Write a function that given two sorted lists, returns a list whith the two lists merged together into one sorted list.

So, for instance, for inputs [1,5,7,8] and [2,3,4,7,9] it should return [1,2,3,4,5,7,7,8,9].

Try and make your code as efficient as possible.

19 Upvotes

39 comments sorted by

View all comments

Show parent comment

3

u/oskar_s May 16 '12

For the easy problems, people can solve them however they want. The problems are supposed to be easy after all. When I put up this problem, I had in mind people implementing something like the merge pass of merge-sort, which takes some actual programming, not just using libraries, which is why I put the "make it as efficient as possible".

But again: however people want to solve it is more or less fine.

3

u/TweenageDream May 16 '12 edited May 17 '12

I never really thought about it in the way you were speaking, I went ahead and researched the algorithms you were talking about a bit, learned a bit, and had some very noticeable results. Thanks for pushing for efficiency, honestly! I'm a big fan of optimization, and small code, as well as efficient and fast code, however i don't know all the theory behind it, but now that i read up on it, it makes sense and is logical. So thank you!

The code:

def add_n_sort(l1, l2)
    (l1 + l2).sort
end

def merge_sort(l1,l2)
    l3 = []
    while 0 < l1.length or 0 < l2.length do
        #*snip and edit for speed*
        if l2[0].nil? or (!l1[0].nil? and l1[0] < l2[0])
            l3 << l1.shift
        else
            l3 << l2.shift
        end
    end
    l3
end

s = Time.now
l1 = [1,5,7,8]
l2 = [2,3,4,7,9]
puts "#{add_n_sort(l1,l2)}"
1e6.to_i.times{ add_n_sort(l1,l2) }
puts "Finished add_n_sort in #{Time.now - s } seconds"

s = Time.now
puts "#{merge_sort(l1,l2)}"
1e6.to_i.times{ merge_sort(l1,l2) }
puts "Finished merge_sort in #{Time.now - s } seconds"

The output:

[1, 2, 3, 4, 5, 7, 7, 8, 9]

Finished add_n_sort in 1.346 seconds

[1, 2, 3, 4, 5, 7, 7, 8, 9]

Finished merge_sort in 0.557 seconds

Finished merge_sort in 0.343 seconds

EDIT Results of an 2 arrays of length 500k ish, one evens, one odds

Finished add_n_sort in 2.419 seconds - 5 sorts

Finished merge_sort in 2.134 seconds - 1,000,000 sorts

-2

u/Maristic May 16 '12

If you want people to implement things directly without calling library code, then I'd suggest you ask for that explicitly.

If you ask for “the most efficient” solution, that often isn't a “code it all myself” solution, and in fact, it's probably bad for people to conflate the two, just as it is bad to think “O(n log n) is worse than O(n)”. (In the latter case, the technically correct version is to say that a Θ(n log n) algorithm is worse than a Θ(n) algorithm, for n > N; but N may be large enough that no one cares in practice; in theory, N could be larger than the factorial of the number of particles in the universe; math doesn't mind big constants).

But yes, I agree that the point here is for people to solve the problems however they wish. Some people are starting out and want a simple challenge. Others want to show that many simple programming problems are actually easy to solve with minimal code in their favorite languages.