From da77d06d16065710a9f3b7855f2d87b13ccf563f Mon Sep 17 00:00:00 2001 From: Mayank Padshala Date: Sun, 16 Nov 2025 22:52:57 +0000 Subject: [PATCH] PreCourse-2 Complete --- Exercise_1.py | 34 +++++++++++++++++++++++----------- Exercise_2.py | 30 +++++++++++++++++++----------- Exercise_3.py | 43 ++++++++++++++++++++++++++++++++++++------- Exercise_4.py | 48 ++++++++++++++++++++++++++++++++++++++++++++---- Exercise_5.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 163 insertions(+), 36 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..eb3d9b51 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,34 @@ -# Python code to implement iterative Binary -# Search. +# Python code to implement iterative Binary Search. # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here - - + while l<=r: + mid = l + ((r-l)//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 +arr = [ 2, 3, 4, 10, 40 ] + +# Function call +result = binarySearch(arr, 0, len(arr)-1, 10) + +if result != -1: + print("Element is present at index % d" % result) +else: + print("Element is not present in array") # Function call -result = binarySearch(arr, 0, len(arr)-1, x) +result = binarySearch(arr, 0, len(arr)-1, 20) -if result != -1: - print "Element is present at index % d" % result +if result != -1: + 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..a4656ddf 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,31 @@ # Python program for implementation of Quicksort Sort -# give you explanation for the approach -def partition(arr,low,high): - - +# Keep on checking if all the elemts smaller than pivot are on the left of i + 1 +# If all elements till i are lesser than pivot, place the pivot at i+1th position and return the index of the pivot. +# Hence we partition by placing the pivot at its place when array is sorted, so all elements to the left of pivot are smaller and all elements to the right of pivot are greater. +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): - +def quickSort(arr,low,high): #write your code here + if low < high: + pi = partition(arr,low,high) + quickSort(arr,0,pi-1) + quickSort(arr,pi+1, high) # Driver code to test above arr = [10, 7, 8, 9, 1, 5] -n = len(arr) +n = len(arr) quickSort(arr,0,n-1) print ("Sorted array is:") for i in range(n): - print ("%d" %arr[i]), - - + print("%d" %arr[i]) \ No newline at end of file diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..87daf106 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -2,19 +2,39 @@ class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data): + self.val = data + self.next = None class LinkedList: - def __init__(self): - + def __init__(self): + self.head = None - def push(self, new_data): + def push(self, new_data): + new_node = Node(new_data) + if not self.head: + self.head = new_node + return + curr = self.head + while curr.next: + curr = curr.next + + curr.next = new_node - # Function to get the middle of - # the linked list - def printMiddle(self): + # Function to get the middle of the linked list + def printMiddle(self): + if not self.head: + print("Linked List is Empty") + return + slow = self.head + fast = slow + while fast.next and fast.next.next: + slow = slow.next + fast = fast.next.next + print(slow.val) + # Driver code list1 = LinkedList() @@ -24,3 +44,12 @@ def printMiddle(self): list1.push(3) list1.push(1) list1.printMiddle() + +list1 = LinkedList() +list1.push(5) +list1.printMiddle() + +list1 = LinkedList() +list1.push(1) +list1.push(2) +list1.printMiddle() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..df6ba163 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,12 +1,52 @@ +def merge(array, left, mid, right): + # Array is sorted from left to mid + leftSortedArray = arr[left:mid+1] + # Array is sorted from mid+1 to right + rightSortedArray = arr[mid+1:right+1] + # Start of writerindex + writerIdx = left + leftIdx = 0 + rightIdx = 0 + + while leftIdx < len(leftSortedArray) and rightIdx < len(rightSortedArray): + if leftSortedArray[leftIdx] < rightSortedArray[rightIdx]: + arr[writerIdx] = leftSortedArray[leftIdx] + leftIdx+=1 + else: + arr[writerIdx] = rightSortedArray[rightIdx] + rightIdx+=1 + writerIdx+=1 + + while leftIdx < len(leftSortedArray): + arr[writerIdx] = leftSortedArray[leftIdx] + leftIdx+=1 + writerIdx+=1 + + while rightIdx < len(rightSortedArray): + arr[writerIdx] = rightSortedArray[rightIdx] + rightIdx+=1 + writerIdx+=1 + +def mergeSortRecursive(arr, left, right): + # We have to sort the array from left to right + if left >= right: + return + # we take the mid + mid = (left + right) // 2 + # Sort the left hafl + mergeSortRecursive(arr, left, mid) + # Sort the right half + mergeSortRecursive(arr, mid+1, right) + # Merge the 2 sorted halves + merge(arr, left, mid, right) + # Python program for implementation of MergeSort def mergeSort(arr): - - #write your code here + mergeSortRecursive(arr, 0, len(arr)-1) # Code to print the list def printList(arr): - - #write your code here + print(arr) # driver code to test the above code if __name__ == '__main__': diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..5e557278 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,48 @@ # Python program for implementation of Quicksort # This function is same in both iterative and recursive -def partition(arr, l, h): - #write your code here +def partition(arr, low, high): + pivot = arr[high] + i = low - 1 # Index of the smaller element + for j in range(low, high): + # If current element is smaller than or equal to pivot + if arr[j] <= pivot: + i = i + 1 + arr[i], arr[j] = arr[j], arr[i] + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 def quickSortIterative(arr, l, h): - #write your code here + # Sorts an array using iterative Quick Sort by managing a stack of sub-array boundaries to be processed. + if not arr: + return + # Create a stack to store low and high indices + # We use a list and append/pop from the end for stack behavior + stack = [] + # Push initial low and high indices + stack.append(l) + stack.append(h) + # Keep popping from stack while it is not empty + while stack: + high = stack.pop() + low = stack.pop() + # Get pivot element's position after partitioning + p = partition(arr, low, high) + # If there are elements on the left side of pivot, push them to stack + if p - 1 > low: + stack.append(low) + stack.append(p - 1) + # If there are elements on the right side of pivot, push them to stack + if p + 1 < high: + stack.append(p + 1) + stack.append(high) + + return arr + +# driver code to test the above code +arr = [10, 80, 30, 90, 40, 50, 70] +print(f"Original array: {arr}") +sortedArr = quickSortIterative(arr, 0, len(arr)-1) +print(f"Sorted array: {sortedArr}") \ No newline at end of file