diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..04a2e98b 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -6,17 +6,27 @@ def binarySearch(arr, l, r, x): #write your code here - + # The Binary Search function checks whether an element exists in a given array by utilizing the left + # and right pointers of the array and updating them based on specific criteria. + while l<=r: + mid = (l+r)//2 + if arr[mid] == x: + return mid + elif arr[mid] < x: + l = mid +1 + else: + r = mid -1 + return -1 # Test array arr = [ 2, 3, 4, 10, 40 ] -x = 10 +x = 4 # Function call result = binarySearch(arr, 0, len(arr)-1, x) if result != -1: - print "Element is present at index % d" % result + print("Element is present at index ", result) else: - print "Element is not present in array" + print ("Element is not present in array") diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..8714e1c7 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -5,12 +5,26 @@ def partition(arr,low,high): #write your code here + # Rearranges the array so that smaller and greater elements are to left side and right side of pivot element respectively. + pivot = arr[high] + i = low - 1 + for j in range(low,high): + if arr[j]