fork(7) download
  1. def merge(arr, l, m, r):
  2. n1 = m - l + 1
  3. n2 = r- m
  4.  
  5. L = [0] * (n1)
  6. R = [0] * (n2)
  7. print(l,m)
  8. # Copy data to temp arrays L[] and R[]
  9. for i in range(0 , n1):
  10. L[i] = arr[l + i]
  11.  
  12. for j in range(0 , n2):
  13. R[j] = arr[m + 1 + j]
  14.  
  15. i = 0 # Initial index of first subarray
  16. j = 0 # Initial index of second subarray
  17. k = l # Initial index of merged subarray
  18.  
  19. while i < n1 and j < n2 :
  20. if L[i] <= R[j]:
  21. arr[k] = L[i]
  22. i += 1
  23. else:
  24. arr[k] = R[j]
  25. j += 1
  26. k += 1
  27.  
  28. # Copy the remaining elements of L[], if there
  29. # are any
  30. while i < n1:
  31. arr[k] = L[i]
  32. i += 1
  33. k += 1
  34.  
  35. # Copy the remaining elements of R[], if there
  36. # are any
  37. while j < n2:
  38. arr[k] = R[j]
  39. j += 1
  40. k += 1
  41.  
  42.  
  43. def mergeSort(arr,l,r):
  44. if l < r:
  45. m = int((l+(r-1))/2)
  46. mergeSort(arr, l, m)
  47. mergeSort(arr, m+1, r)
  48. merge(arr, l, m, r)
  49.  
  50.  
  51. arr = [12, 11, 13, 5, 6, 7]
  52. n = len(arr)
  53. print ("Given array is")
  54. for i in range(n):
  55. print ("%d" %arr[i]),
  56.  
  57. mergeSort(arr,0,n-1)
  58. print ("\n\nSorted array is")
  59. for i in range(n):
  60. print ("%d" %arr[i]),
Success #stdin #stdout 0.01s 7208KB
stdin
Standard input is empty
stdout
Given array is
12 11 13 5 6 7 (1, 1)
(0, 0)
(4, 4)
(3, 3)
(0, 2)


Sorted array is
5 6 7 11 12 13