44# in S whose sum is exactly x. 
55
66def  checkSum (array , sum ):
7-     # sort the array in descending order 
8-     array  =  quickSort (array )
7+     # sort the array in ascending order 
8+     # new changes : made use of Python's inbuilt Merge Sort method 
9+     # Reason for such change : Worst case Time complexity of Quick Sort is O(n^2) whereas Worst Case Complexity of Merge Sort is O(nlog(n)) 
10+     array  =  sorted (array )
911
1012    leftIndex  =  0 
1113    rightIndex  =  len (array ) -  1 
@@ -20,14 +22,14 @@ def checkSum(array, sum):
2022
2123    return  False , False 
2224
23- def  quickSort (array ):
24-     if  len (array ) <=  1 :
25-         return  array 
26-     pivot  =  array [len (array ) //  2 ]
27-     left  =  [x  for  x  in  array  if  x  <  pivot ]
28-     middle  =  [x  for  x  in  array  if  x  ==  pivot ]
29-     right  =  [x  for  x  in  array  if  x  >  pivot ]
30-     return  quickSort (left ) +  middle  +  quickSort (right )
25+ ## def quickSort(array):
26+ ##     if len(array) <= 1:
27+ ##         return array
28+ ##     pivot = array[len(array) // 2]
29+ ##     left = [x for x in array if x < pivot]
30+ ##     middle = [x for x in array if x == pivot]
31+ ##     right = [x for x in array if x > pivot]
32+ ##     return quickSort(left) + middle + quickSort(right)
3133
3234if  __name__  ==  '__main__' :
3335    myArray  =  [10 , 20 , 30 , 40 , 50 ]
@@ -37,4 +39,4 @@ def quickSort(array):
3739    if (number1  and  number2 ):
3840        print ('Array has elements:' , number1 , 'and' , number2 , 'with sum:' , sum )
3941    else :
40-         print ('Array doesn\' t has  elements with the sum:' , sum )
42+         print ('Array doesn\' t have  elements with the sum:' , sum )
0 commit comments