the art of
Algorithm
Notes on Analysis and Design



Merge Sort

Function to implement merge sort

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
def merge(arr,left,mid,right):
    n1=left-mid+1
    n2=right-mid
    #creating two empty array of sizes n1 and n2 
    arr1=[0]*n1
    arr2=[0]*n2
    #copying data of arr in both the arrays
    for i in range(n1):
        arr1[i]=arr[i]
    for i in range(n2):
        arr2[i]=arr[i]
    i=0
    j=0
    k=l
    #while loop for merging two arrays back in a single arrays
    while i<n1 and j<n2:
         if arr1[i]<arr2[j]:
            arr[k]=arr1[i]
            i=i+1
        if arr2[j]<arr1[i]:
            arr[k]=arr2[j]
            j=j+1
        k=k+1
    #copying remaining elements if any
    while i<n1:
        arr[k]=arr1[i]
        i=i+1
        k=k+1
    while j<n2:
        arr[k]=arr2[j]
        j=j+1
        k=k+1
def mergesort(arr,left,right):
    if left<right:
        mid=(l+r)/2
        #sort for first halves
        mergesort(arr,left,mid)
        #sort for second halves
        mergesort(arr,mid+1,right)
        merge(arr,left,mid,right)

#main program to test above function
arr=map(int,raw_input().strip().split(' '))
print ("array before sorting is")
for i in range(len(arr)):
    print arr[i],
#calling mergesort function
print ("array after sorting is")
for i in range(len(arr)):
    print arr[i],