4
4
# in S whose sum is exactly x.
5
5
6
6
def 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)
9
11
10
12
leftIndex = 0
11
13
rightIndex = len(array) - 1
@@ -20,14 +22,14 @@ def checkSum(array, sum):
20
22
21
23
return False, False
22
24
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)
31
33
32
34
if __name__ == '__main__':
33
35
myArray = [10, 20, 30, 40, 50]
@@ -37,4 +39,4 @@ def quickSort(array):
37
39
if(number1 and number2):
38
40
print('Array has elements:', number1, 'and', number2, 'with sum:', sum)
39
41
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