Advertisement
lodha1503

Untitled

Mar 13th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. def print_function(arr,low,high):
  2.     for i in range(low,high+1):
  3.         print(arr[i],end=" ")
  4.     print()
  5.    
  6.  
  7.  
  8. def partition(arr,low,high):
  9.     pivot=high
  10.     i=low-1
  11.  
  12.     for j in range(low,high):
  13.         if (arr[j]<arr[pivot]):
  14.             i=i+1
  15.             temp=arr[i]
  16.             arr[i]=arr[j]
  17.             arr[j]=temp
  18.            
  19.      
  20.     temp=arr[i+1]
  21.     arr[i+1]=arr[pivot]
  22.     arr[pivot]=temp
  23.     print_function(arr,0,len(arr)-1)      
  24.     return i+1  
  25.  
  26. def InsertionSort(arr,low,high):
  27.  
  28.     for i in range(low+1,high+1):
  29.         var=i
  30.         for j in range(i-1,-1,-1):
  31.             if(arr[var]<arr[j]):
  32.                 temp=arr[j]
  33.                 arr[j]=arr[var]
  34.                 arr[var]=temp
  35.                 var=j
  36.     #print_function(arr,0,len(arr)-1)  
  37.     return      
  38.    
  39. def QuickSort(arr,low,high):
  40.  
  41.     global T
  42.     if(low<high):
  43.         p=partition(arr,low,high)
  44.  
  45.         l1=p-low
  46.         l2=high-p
  47.         if(l2<T and l2!=0):
  48.             #print_function(arr,0,len(arr)-1)
  49.             InsertionSort(arr,p+1,high)
  50.            
  51.         if (l1<T and l1!=0):
  52.             #print_function(arr,0,len(arr)-1)
  53.             InsertionSort(arr,low,p-1)  
  54.  
  55.            
  56.         if(l2>=T):
  57.             #print_function(arr,0,len(arr)-1)
  58.             QuickSort(arr,p+1,high)
  59.            
  60.            
  61.         if(l1>=T ):
  62.             #print_function(arr,0,len(arr)-1)
  63.             QuickSort(arr,low,p-1)
  64.      
  65.        
  66.            
  67.     return
  68.  
  69.  
  70.  
  71. A=list(map(int,input().split()))
  72. N=A[0]
  73. global T
  74. T=A[1]
  75. arr=[A[i+2] for i in range(0,N)]
  76. low=0
  77. high=len(arr)-1
  78.  
  79. if(high-low+1<T):
  80.     InsertionSort(arr,low,high)
  81. else:
  82.     QuickSort(arr,low,high)
  83.  
  84. print_function(arr,low,high)
  85.  
  86.  
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement