Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Smelly imperative process of merging the two... </p> <pre><code>a = [1,3,7,11] b = [2,4,6,14] c = merge_sorted_arrays(a,b) def merge_sorted_arrays(a,b) a.reverse! b.reverse! output = [] loop do break if a.empty? || b.empty? output &lt;&lt; (a.last &lt; b.last ? a.pop : b.pop) end return output + a.reverse + b.reverse end </code></pre> <p>Maybe using .slice! to take the first element would be better than reversing and popping?</p> <p>====================</p> <p><em>edited after the fourth comment:</em></p> <p>Right... I've had another play, but need to get on with real work or I'll get fired ;-)</p> <p>On a large array of integers, my original method works faster than using sort_by, but after filling the arrays with 100,000 OpenStruct objects, and sorting on an attribute, the sort_by was 100-times faster.</p> <p>Here's my benchmarking results:</p> <pre><code>def pop_merge_sorted_arrays(array1,array2) array1.reverse! array2.reverse! output = [] loop do break if array1.empty? || array2.empty? output &lt;&lt; (array1.last.my_field &lt; array2.last.my_field ? array1.pop : array2.pop) end return output + array1.reverse + array2.reverse end def shift_merge_sorted_arrays(array1,array2) output = [] loop do break if array1.empty? || array2.empty? output &lt;&lt; (array1.first.my_field &lt; array2.first.my_field ? array1.shift : array2.shift) end return output + array1 + array2 end def slice_merge_sorted_arrays(array1,array2) output = [] loop do break if array1.empty? || array2.empty? output &lt;&lt; (array1.first.my_field &lt; array2.first.my_field ? array1.slice!(0) : array2.slice!(0)) end return output + array1 + array2 end a=(1..100000).map{|x|OpenStruct.new(:my_field =&gt; rand)}.sort_by(:my_field) b=(1..100000).map{|x|OpenStruct.new(:my_field =&gt; rand)}.sort_by(:my_field) # method 1 t=Time.now;w=pop_merge_sorted_arrays(a.clone,b.clone);puts Time.now-t # average of five runs: 185.96seconds # method 2 t=Time.now;x=shift_merge_sorted_arrays(a.clone,b.clone);puts Time.now-t # average of five runs: 0.77seconds # method 3 t=Time.now;y=slice_merge_sorted_arrays(a.clone,b.clone);puts Time.now-t # average of five runs: 8.46seconds # method 4 t=Time.now;z=(a.clone + b.clone).sort_by(&amp;:my_field);puts Time.now-t # average of five runs: 2.13seconds </code></pre> <p>So the upshot seems to be that you can write a method that will shuffle them while maintaining order that will run quite quickly (and there's probably some more efficiencies to squeeze out of the shift_merge method, but for the extra benefit, is it really worth <em>not</em> just bunging them together and using sort_by for the ease of it? :-)</p> <p>I hope this doesn't count as a digression...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload