mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-29 20:34:27 +01:00
Add merge sort
This commit is contained in:
parent
aa4dfbeefb
commit
df6fdd0a14
1 changed files with 32 additions and 0 deletions
32
Sorting/merge_sort.rb
Normal file
32
Sorting/merge_sort.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
def merge_sort(array)
|
||||||
|
return array if array.length <= 1
|
||||||
|
mid = array.length / 2
|
||||||
|
first_array = array.slice(0..mid - 1)
|
||||||
|
second_array = array.slice(mid..-1)
|
||||||
|
|
||||||
|
first_array = merge_sort first_array
|
||||||
|
second_array = merge_sort second_array
|
||||||
|
|
||||||
|
# merge
|
||||||
|
result = []
|
||||||
|
until first_array.empty? and second_array.empty?
|
||||||
|
if first_array.empty?
|
||||||
|
result.concat(second_array)
|
||||||
|
second_array.clear
|
||||||
|
elsif second_array.empty?
|
||||||
|
result.concat(first_array)
|
||||||
|
first_array.clear
|
||||||
|
else
|
||||||
|
if first_array.first < second_array.first
|
||||||
|
result << first_array.shift
|
||||||
|
else
|
||||||
|
result << second_array.shift
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Enter a list of numbers seprated by space"
|
||||||
|
list = gets
|
||||||
|
print merge_sort list.split(" ").map(&:to_i)
|
Loading…
Add table
Reference in a new issue