Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- https://practice.geeksforgeeks.org/problems/merge-sort/1
- class Solution:
- def mergeSort(self,arr, l, r):
- def use(arr):
- if len(arr)<=1:
- return
- mid=len(arr)//2
- L=arr[:mid]
- R=arr[mid:]
- use(L)
- use(R)
- m,n=len(L),len(R)
- i=j=k=0
- while i<m and j<n:
- if L[i]<R[j]:
- arr[k]=L[i]
- i=i+1
- else:
- arr[k]=R[j]
- j=j+1
- k=k+1
- while i<m:
- arr[k]=L[i]
- i=i+1
- k=k+1
- while j<n:
- arr[k]=R[j]
- j=j+1
- k=k+1
- return arr
- return use(arr)
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- if __name__ == "__main__":
- t=int(input())
- for i in range(t):
- n=int(input())
- arr=list(map(int,input().split()))
- Solution().mergeSort(arr, 0, n-1)
- for i in range(n):
- print(arr[i],end=" ")
- print()
- # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement