-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.rb
41 lines (39 loc) · 922 Bytes
/
merge_sort.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def merge(left, right)
result = []
while left.size > 0 and right.size > 0
if left[0] <= right[0]
result << left[0]
left = left[1..-1]
else
result << right[0]
right = right[1..-1]
end
end
if left.size > 0
result.concat left
end
if right.size > 0
result.concat right
end
return result
end
def merge_sort(arr)
left, right, result = []
if arr.size <= 1
return arr
else
middle = arr.size / 2
left = arr[0..middle - 1]
right = arr[middle..-1]
left = merge_sort(left)
right = merge_sort(right)
if left.last <= right[0]
left.concat right
return left
end
result = merge(left, right)
return result
end
end
arr = [1,4,54,2545,23,524,5,2435,432,543,25,2435,2]
puts merge_sort(arr)