forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Merge_Sort.rb
60 lines (51 loc) · 1.13 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$temp = []
# Conquer
def conquer_merge(array, left, right, mid)
#temp = [None] * len(array)
i = left
j = mid + 1
k = left
while i <= mid and j <= right
if array[i] <= array[j]
$temp[k] = array[i]
i += 1
else
$temp[k] = array[j]
j += 1
end
k += 1
end
while i <= mid
$temp[k] = array[i]
i += 1
k += 1
end
while j <= right
$temp[k] = array[j]
j += 1
k += 1
end
while left <= right
array[left] = $temp[left]
left += 1
end
end
# Divide array into halves
def divide(array, left, right)
if left < right
mid = (left + (right - left) / 2).to_i
divide(array, left, mid)
divide(array, mid + 1, right)
conquer_merge(array, left, right, mid)
end
end
def Merge_Sort(array)
$temp = [0] * array.length
divide(array, 0, array.length - 1)
$temp.clear
end
array = [2, 4, 3, 1, 6, 8, 4] #Hard Coded Array
Merge_Sort(array)
print(array)
# Output
# 1 2 3 4 4 6 8