-
Notifications
You must be signed in to change notification settings - Fork 252
/
MergeSort.rb
66 lines (55 loc) · 1.73 KB
/
MergeSort.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
61
62
63
64
65
66
#Merge Sort (divides input array in two halves, calls itself for the two halves and then merges the two sorted halves recursively)
#Time-complexity: O(nlogn),Auxiliary Space: O(n),Stable
#Algorithmic Paradigm: Divide and Conquer
#Uses: Sorting linked lists(implementation details differ), Counting inversions in array
#MergeSort(arr[], l, r)
#If r > l
# 1. Find the middle point to divide the array into two halves:
# middle m = (l+r)/2
# 2. Call mergeSort for first half:
# Call mergeSort(arr, l, m)
# 3. Call mergeSort for second half:
# Call mergeSort(arr, m+1, r)
# 4. Merge the two halves sorted in step 2 and 3:
# Call merge(arr, l, m, r)
#Merge Sort Logic
# Logic to merge two sub arrays
def merge(a,l,m,r)
n1 = m-l+1
left = Array.new(n1) # Created temp array for storing left subarray
n2 = r-m
right = Array.new(n2) # Created temp array for storing right subarray
for i in 0...n1 #Copy values from left subarray to temp array
left[i]= a[l+i]
end
for i in 0...n2 #Copy values from Right subarray to temp array
right[i]= a[m+1+i]
end
i=0
j=0
# Comparing and merging two sub arrays
for k in l..r
if i>=n1
a[k]=right[j]
j+=1
elsif j>=n2
a[k]=left[i]
i+=1
elsif left[i]>right[j]
a[k]=right[j]
j+=1
else
a[k]=left[i]
i+=1
end
end
end
# Merge Sort
def merge_sort(a,l,r)
if l<r
m=l+(r-l)/2
merge_sort(a,l,m)
merge_sort(a,m+1,r)
merge(a,l,m,r)
end
end