SHARE
TWEET

MergeSort

michalkowalczyk Dec 2nd, 2018 (edited) 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Dec  2 20:29:31 2018
  4.  
  5. @author: micha
  6. """
  7.  
  8. A=[]
  9. from random import randint    
  10. for i in range(0,100):
  11.     A.append(randint(1,100))    #dodawnie losowanyc elemetow
  12.    
  13. print(A)
  14. print()
  15. #from math import floor
  16. def MergeSort(A,p,k):
  17.     if(p<k):
  18.         q=(p+k)//2
  19.         #q = floor((p+k)/2)
  20.         MergeSort(A,p,q)
  21.         MergeSort(A,q+1,k)
  22.         Merge(A,p,q,k)
  23.        
  24. def Merge(A,p,q,k):
  25.     n1=q-p+1
  26.     n2=k-q
  27.     L=[0]*(n1)
  28.     R=[0]*(n2)
  29.     """
  30.     L=[]
  31.     R=[]
  32.     """
  33.    
  34.  
  35.     """for i in range(0,n1):
  36.        L.append(A[p+i-1])
  37.    for j in range(0,n2):
  38.        R.append(A[q+j+1])
  39.       """
  40.     for i in range(0,n1):
  41.         L[i]=A[p+i]
  42.     for j in range(0,n2):
  43.        R[j]=A[q+1+j]
  44.    
  45.    
  46.     i=j=0
  47.     z=p
  48.     while(i<len(L) and j<len(R)):
  49.         if(L[i]<R[j]):
  50.             A[z]=L[i]
  51.             i+=1
  52.            
  53.         else:
  54.             A[z]=R[j]
  55.             j+=1
  56.         z+=1
  57.     while(i<len(L)):
  58.         A[z]=L[i]
  59.         i+=1
  60.         z+=1
  61.     while(j<len(R)):
  62.         A[z]=R[j]
  63.         j+=1
  64.         z+=1
  65.        
  66. MergeSort(A,0,len(A)-1)
  67. print(A)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top