-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.rb
More file actions
55 lines (45 loc) · 1.3 KB
/
MergeSort.rb
File metadata and controls
55 lines (45 loc) · 1.3 KB
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
# pg 271 of Algo 4th Edition
def merge!(arr, low, mid, high, aux_arr=[])
(0...arr.length).each do |i| # Copies the arr into the aux_arr
aux_arr[i] = arr[i]
end
i = low
j = mid + 1
(low..high).each do |k|
if (i > mid)
arr[k] = aux_arr[j]
j += 1 # Wish there was a clean way to do j++/i++
elsif (j > high)
arr[k] = aux_arr[i]
i += 1
elsif (aux_arr[j] < aux_arr[i])
arr[k] = aux_arr[j]
j += 1
else
arr[k] = aux_arr[i]
i += 1
end
end
# p aux_arr.object_id # Shows that no new aux_arrs are created
arr # Line needed for base case, won't work for <=2 element arrays without it
end
# pg 273 of Algo 4th Edition
def mergesort!(arr, low=0, high = arr.length-1, aux_arr=arr.dup) # Did not know about the arr.length as a param
# aux_arr = arr.dup # Don't need this line if I create the aux_arr in mergesort! params
# p aux_arr.object_id # Shows that no new aux_arrs are created
mid = low + (high - low)/2
return merge!(arr,low,mid,high, aux_arr) if low+1 >= high # Base case
mergesort!(arr, low, mid, aux_arr)
mergesort!(arr, mid+1, high, aux_arr)
merge!(arr,low, mid, high, aux_arr)
arr
end
1000.times do |x|
arr = []
rand(300).times do |y|
arr << rand
end
if (arr.sort != mergesort!(arr))
puts "Problem!"
end
end