forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort.coffee
More file actions
37 lines (27 loc) · 737 Bytes
/
Merge_Sort.coffee
File metadata and controls
37 lines (27 loc) · 737 Bytes
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
Merge_Sort = (array) ->
if array.length <= 1
return array
mid = Math.floor(array.length / 2)
# Divide array into two halves
left = Merge_Sort(array.slice 0, mid)
right = Merge_Sort(array.slice mid)
result = []
p1 = p2 = 0
# Conquer and merge array
while true
if p1 >= left.length
if p2 >= right.length
return result
result.push right[p2]
p2 += 1
else if p2 >= right.length or left[p1] < right[p2]
result.push left[p1]
p1 += 1
else
result.push right[p2]
p2 += 1
array = [2, 4, 3, 1, 6, 8, 4]
console.log Merge_Sort array
### Output
[ 1, 2, 3, 4, 4, 6, 8 ]
###