Advertisement
lodha1503

Untitled

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