From 175f5adf423556341bb6541cc09ff069765e39e5 Mon Sep 17 00:00:00 2001 From: Bhuvan Kurra Date: Mon, 22 Dec 2025 21:57:51 -0500 Subject: [PATCH] Complete Python solutions for Exercises 1-5 --- Exercise_1.py | 14 +++++++++++--- Exercise_2.py | 18 ++++++++++++++---- Exercise_3.py | 17 ++++++++++++++--- Exercise_4.py | 32 ++++++++++++++++++++++++++++++-- Exercise_5.py | 20 ++++++++++++++++++-- 5 files changed, 87 insertions(+), 14 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..81b61c15 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -5,7 +5,15 @@ # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here + while l <= r: + m = l + (r - l) // 2 + if arr[m] == x: + return m + if arr[m] < x: + l = m + 1 + else: + r = m - 1 + return -1 @@ -17,6 +25,6 @@ def binarySearch(arr, l, r, x): 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 % d" % 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..b9dd63b6 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -4,20 +4,30 @@ def partition(arr,low,high): - #write your code here + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i+1], arr[high] = arr[high], arr[i+1] + return i + 1 # Function to do Quick sort def quickSort(arr,low,high): - #write your code here + if low < high: + pi = partition(arr, low, high) + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) # Driver code to test above arr = [10, 7, 8, 9, 1, 5] n = len(arr) quickSort(arr,0,n-1) -print ("Sorted array is:") +print("Sorted array is:") for i in range(n): - print ("%d" %arr[i]), + print("%d" % arr[i]) diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..29b43851 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,18 +3,29 @@ class Node: # Function to initialise the node object def __init__(self, data): - + self.data = data + self.next = None class LinkedList: def __init__(self): - + self.head = None def push(self, new_data): - + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node # Function to get the middle of # the linked list def printMiddle(self): + if self.head is None: + return + slow = self.head + fast = self.head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + print(slow.data) # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..91d9f87d 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,12 +1,40 @@ # Python program for implementation of MergeSort def mergeSort(arr): - #write your code here + if len(arr) > 1: + mid = len(arr) // 2 + L = arr[:mid] + R = arr[mid:] + + mergeSort(L) + mergeSort(R) + + i = j = k = 0 + while i < len(L) and j < len(R): + if L[i] <= R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 # Code to print the list def printList(arr): - #write your code here + for i in range(len(arr)): + print(arr[i], end=" ") + print() # driver code to test the above code if __name__ == '__main__': diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..6f82f81d 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -2,9 +2,25 @@ # This function is same in both iterative and recursive def partition(arr, l, h): - #write your code here + pivot = arr[h] + i = l - 1 + for j in range(l, h): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i+1], arr[h] = arr[h], arr[i+1] + return i + 1 def quickSortIterative(arr, l, h): - #write your code here + stack = [] + stack.append((l, h)) + while stack: + l, h = stack.pop() + if l < h: + p = partition(arr, l, h) + if p - 1 > l: + stack.append((l, p - 1)) + if p + 1 < h: + stack.append((p + 1, h))