asda

Php code posted by asda
created at 21 Nov 08:26, updated at 21 Nov 19:59

Edit | Back
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
def merge_sort(m)
    if m.length <= 1
        return m
    end

    middle = m.length / 2
    left = m[0,middle]
    right = m[middle..-1]

    left = merge_sort(left)
    right = merge_sort(right)
    merge(left, right)
end
 
def merge(left, right)
    result = []
 
    until left.empty? || right.empty?
        # change the direction of this comparison to change the direction of the sort
        if left.first <= right.first
            result << left.shift
        else
            result << right.shift
        end
    end
 
    unless left.empty?
    
        result += left
    end
    unless right.empty?
        result += right
    end
    result
end
697 Bytes in 3 ms with coderay