From f9dac3bda63c3233fbe1c5faa1ea4d2c60a83c0b Mon Sep 17 00:00:00 2001 From: Neeraj Date: Wed, 24 Dec 2025 19:05:03 -0800 Subject: [PATCH] Pre Course-2 Done --- Exercise_1.py | 20 ++++++++++++++++++-- Exercise_2.py | 29 +++++++++++++++++++++++++---- Exercise_3.py | 22 +++++++++++++++++++--- Exercise_4.py | 39 +++++++++++++++++++++++++++++++++++++++ Exercise_5.py | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 9 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..cb46452c 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -6,7 +6,23 @@ def binarySearch(arr, l, r, x): #write your code here + while l <= r: + mid = l + (r - l) // 2 + + # Check if x is present at mid + if arr[mid] == x: + return mid + + # If x is greater, ignore left half + elif arr[mid] < x: + l = mid + 1 + + # If x is smaller, ignore right half + else: + r = mid - 1 + # If we reach here, then the element was not present + return -1 # Test array @@ -17,6 +33,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..25241840 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -3,21 +3,42 @@ # give you explanation for the approach def partition(arr,low,high): - - #write your code here + # Choose the rightmost element as pivot + pivot = arr[high] + + # Index of smaller element (indicates right position of pivot) + i = low - 1 + + for j in range(low, high): + # If current element is smaller than or equal to pivot + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + + # Place pivot at its correct position + 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 is partitioning index, arr[pi] is now at right place + pi = partition(arr, low, high) + + # Recursively sort elements before and after partition + 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], end=" ") +print() diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..58c873f0 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,18 +3,34 @@ 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): + def printMiddle(self): + if self.head is None: + print("List is empty") + return + + slow_ptr = self.head + fast_ptr = self.head + + while fast_ptr and fast_ptr.next: + slow_ptr = slow_ptr.next + fast_ptr = fast_ptr.next.next + + print("Middle element is:", slow_ptr.data) # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..0138a4e7 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -2,11 +2,50 @@ def mergeSort(arr): #write your code here + if len(arr) > 1: + # Finding the mid of the array + mid = len(arr) // 2 + + # Dividing the array elements into 2 halves + left_half = arr[:mid] + right_half = arr[mid:] + + # Sorting the first half + mergeSort(left_half) + + # Sorting the second half + mergeSort(right_half) + + i = j = k = 0 + + # Copy data to temp arrays left_half[] and right_half[] + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[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..ec225616 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -3,8 +3,44 @@ # This function is same in both iterative and recursive def partition(arr, l, h): #write your code here + # Choose the rightmost element as pivot + pivot = arr[h] + + # Index of smaller element (indicates right position of pivot) + i = l - 1 + + for j in range(l, h): + # If current element is smaller than or equal to pivot + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + + # Place pivot at its correct position + arr[i + 1], arr[h] = arr[h], arr[i + 1] + return i + 1 def quickSortIterative(arr, l, h): #write your code here + # Create an auxiliary stack + stack = [] + + # Push initial values of l and h to stack + stack.append((l, h)) + + # Keep popping from stack while it is not empty + while stack: + # Pop l and h + l, h = stack.pop() + + # Set pivot element at its correct position + p = partition(arr, l, h) + + # If there are elements on left side of pivot, push left side to stack + if p - 1 > l: + stack.append((l, p - 1)) + + # If there are elements on right side of pivot, push right side to stack + if p + 1 < h: + stack.append((p + 1, h))