0
\$\begingroup\$

I've implemented merge sort an integer array. Should I change them to more meaningful names? Overall, any further suggestions on this code? I would like to improve my coding style and to shorten the code.

class MergeSortAlgorithm
def sort(numbers)
if numbers.size <= 1
 return numbers
 end
 array_size = numbers.size
half_of_size = (array_size / 2).round
left_array = numbers.take(half_of_size)
 right_array = numbers.drop(half_of_size)
 sorted_left_array = sort(left_array)
 sorted_right_array = sort(right_array)
 merge(sorted_left_array, sorted_right_array)
 end
 def merge(left_array, right_array)
 if right_array.empty?
 return left_array 
 end
 if left_array.empty?
 return right_array 
 end
 smallest_number = if left_array.first <= right_array.first
 left_array.shift
 else
 right_array.shift
 end
 recursive = merge(left_array, right_array)
 [smallest_number].concat(recursive)
 end
 end
 merge_sort = MergeSortAlgorithm.new
 puts merge_sort.sort([4, 92, 1, 39, 19, 93, 49, 10].shuffle)
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 2, 2017 at 7:25
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Your indentation is wrong. Please ensure that you have represented the code correctly. The indentation looks wrong. (The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.) \$\endgroup\$ Commented Jul 2, 2017 at 12:55
  • 1
    \$\begingroup\$ Why don't you update your code and maybe we can continue the discussion from there, for example we could do some validation of the array, what if someone enters [-1] or ['a', :1, Object] etc? \$\endgroup\$ Commented Jul 4, 2017 at 9:24

1 Answer 1

2
\$\begingroup\$

First thing you can do is to improve your code layout, I recommend you use the code style that most rubyists use:

https://github.com/bbatsov/ruby-style-guide/blob/master/README.md#source-code-layout

The next best thing is to write some unit tests for your code, you can use the built in Minitest library, or you can try Rspec. You don't need complicated tests, just some assertions to make sure the sorting is indeed done correctly, which gives you freedom to refactor.

You can also use the ruby convention and change some of your if's in one liners.

return numbers if numbers.size <= 1

Besides that, as long as the code works as expected, the names you choose are good, but depending on their taste, people will like it or not.

t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
answered Jul 3, 2017 at 15:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.