SnowArraySort 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/SnowArraySort/merge_array_sort_snow.rb +24 -25
- data/lib/SnowArraySort/version.rb +1 -1
- metadata +1 -1
@@ -2,38 +2,37 @@ module SnowArraySort
|
|
2
2
|
class MASort
|
3
3
|
include SnowArraySort
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def mergesort(array)
|
6
|
+
def merge(left_sorted, right_sorted)
|
7
|
+
res = []
|
8
|
+
l = 0
|
9
|
+
r = 0
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
loop do
|
12
|
+
break if r >= right_sorted.length and l >= left_sorted.length
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
14
|
+
if r >= right_sorted.length or (l < left_sorted.length and left_sorted[l] < right_sorted[r])
|
15
|
+
res << left_sorted[l]
|
16
|
+
l += 1
|
17
|
+
else
|
18
|
+
res << right_sorted[r]
|
19
|
+
r += 1
|
21
20
|
end
|
22
|
-
|
23
|
-
return res
|
24
21
|
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
right_sorted = mergesort_iter(array_sliced[mid+1..-1])
|
32
|
-
return merge(left_sorted, right_sorted)
|
33
|
-
end
|
23
|
+
return res
|
24
|
+
end
|
25
|
+
|
26
|
+
def mergesort_iter(array_sliced)
|
27
|
+
return array_sliced if array_sliced.length <= 1
|
34
28
|
|
35
|
-
|
29
|
+
mid = array_sliced.length/2 - 1
|
30
|
+
left_sorted = mergesort_iter(array_sliced[0..mid])
|
31
|
+
right_sorted = mergesort_iter(array_sliced[mid+1..-1])
|
32
|
+
return merge(left_sorted, right_sorted)
|
36
33
|
end
|
34
|
+
|
35
|
+
mergesort_iter(array)
|
37
36
|
end
|
38
37
|
end
|
39
38
|
end
|