Created
January 24, 2014 22:10
-
-
Save gringocl/8607771 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Attempting to implement a mergesort algorithm, pry exited with the included error message. The corresponding algorithm is attached also. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MergeSort | |
def initialize(array_to_sort) | |
@array_to_sort = array_to_sort | |
end | |
def recursive_divisor_and_collector(array_to_sort) | |
@array_to_sort if @array_to_sort.size <= 1 | |
array_divided = @array_to_sort.slice!( (@array_to_sort.size/2).round, @array_to_sort.length ) | |
left_unsorted_array = recursive_divisor_and_collector(@array_to_sort) | |
right_unsorted_array = recursive_divisor_and_collector(array_divided) | |
sorted_array = merge(left_unsorted_array, right_unsorted_array) | |
end | |
def self.merge(left_unsorted_array, right_unsorted_array) | |
sorted_array = [] | |
until left_unsorted_array || right_unsorted_array <= 1 | |
if left_unsorted_array[0] <= right_unsorted_array[0] | |
sorted_array << left_unsorted_array[0] | |
elsif right_unsorted_array[0] <= left_unsorted_array[0] | |
sorted_array << right_unsorted_array[0] | |
end | |
end | |
sorted_array + left_unsorted_array + right_unsorted_array | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2.1.0 (main):0 > load 'mergeSort.rb' | |
=> true | |
2.1.0 (main):0 > merging = MergeSort.new([4,7,23,9,0,3,2,16,5]) | |
=> #<MergeSort:0x007f9e4b3b0f60 @array_to_sort=[4, 7, 23, 9, 0, 3, 2, 16, 5]> | |
2.1.0 (main):0 > merging.recursive_divisor_and_collector(@array_to_sort) | |
/Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:523:in `attr_accessor': can't modify frozen Class (RuntimeError) | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:523:in `singleton class' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:522:in `last_exception=' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:280:in `rescue in re' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:270:in `re' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:254:in `rep' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:234:in `block (3 levels) in repl' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:232:in `loop' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:232:in `block (2 levels) in repl' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:231:in `catch' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:231:in `block in repl' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:230:in `catch' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_instance.rb:230:in `repl' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/pry_class.rb:170:in `start' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/cli.rb:201:in `block in <top (required)>' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/cli.rb:70:in `call' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/cli.rb:70:in `block in parse_options' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/cli.rb:70:in `each' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/lib/pry/cli.rb:70:in `parse_options' | |
from /Users/gringocl/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/pry-0.9.12.4/bin/pry:16:in `<top (required)>' | |
from /Users/gringocl/.rbenv/versions/2.1.0/bin/pry:23:in `load' | |
from /Users/gringocl/.rbenv/versions/2.1.0/bin/pry:23:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't implicilty return my array in the first line. Ruby implicitly returns a value but at the end of a statement