From 3bedcf4c45c824d24ccd49584431c8f5a65255cd Mon Sep 17 00:00:00 2001 From: Sarthak Vaidya Date: Sun, 17 Aug 2025 22:52:11 -0400 Subject: [PATCH] PreCourse2_Done --- Exercise_1.py | 26 ++++++++++++++++++++------ Exercise_2.py | 24 ++++++++++++++++++++---- Exercise_3.py | 35 +++++++++++++++++++++++++---------- Exercise_4.py | 45 +++++++++++++++++++++++++++++++++++++++------ Exercise_5.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 26 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..1cd79812 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,12 +1,26 @@ -# Python code to implement iterative Binary -# Search. +# Python code to implement iterative Binary Search. + +# Time Complexity :O(logn) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None +# Your code here along with comments explaining your approach: Here we use a while loop to continue the process of comparing the key and splitting the search space in two halves. Based if the key is greater than the middle element, we search the right half of the array, otherwise we search the left half. This process continues until we find the key or until the search space is empty. # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here - + low = 0 + high = len(arr) - 1 + while low <= high: + mid = (low + high) // 2 + if arr[mid] == x: + return mid + elif arr[mid] < x: + low = mid + 1 + else: + high = mid - 1 + return -1 # Test array @@ -17,6 +31,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..9a4c2556 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,14 +1,30 @@ # Python program for implementation of Quicksort Sort # give you explanation for the approach +# Time Complexity :O(N^2) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None +# Your code here along with comments explaining your approach: Select the rightmost element as the pivot. Rearrange the array so that elements smaller than the pivot are on the left, and elements greater than the pivot are on the right. Return the pivot’s index. Recursively apply the same process to the left and right sub-arrays created by the pivot. 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 = 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): + if low < high: + pi = partition(arr, low, high) + quickSort(arr, low, pi -1) + quickSort(arr, pi + 1, high) + #write your code here diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..e8810ce2 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,21 +1,36 @@ +# Time Complexity :O(N) +# Space Complexity :O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None +# Your code here along with comments explaining your approach: Traverse linked list using two pointers. Move one pointer by one and another pointer by two. When the fast pointer reaches the end slow pointer will reach middle of the linked list. + # Node class class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data): + self.data = data + self.next = None class LinkedList: - def __init__(self): - - - def push(self, new_data): - - - # Function to get the middle of - # the linked list - def printMiddle(self): + 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): + slow = self.head + fast = self.head + if self.head is not None: + while (fast is not None and fast.next is not None): + fast = fast.next.next + slow = slow.next + print("The middle element is: ", slow.data) # Driver code list1 = LinkedList() list1.push(5) diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..6ea93a2c 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,13 +1,46 @@ -# Python program for implementation of MergeSort +# Python program for implementation of MergeSort +# Time Complexity :O(n log n) +# Space Complexity :O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None +# Your code here along with comments explaining your approach: The code implements Merge Sort, a divide-and-conquer algorithm that recursively splits the array into two halves until each subarray has one element. The mergeSort function then merges these sorted halves by comparing elements one by one and placing them in the correct order. The printList function is just for displaying the array, and the driver code shows the array before and after sorting. + + def mergeSort(arr): - #write your code here - + if len(arr) > 1: + mid = len(arr) // 2 + left = arr[:mid] + right = arr[mid:] + mergeSort(left) + mergeSort(right) + + i = j = k = 0 + while i < len(left) and j < len(right): + if left[i] < right[j]: + arr[k] = left[i] + i += 1 + else: + arr[k] = right[j] + j +=1 + k += 1 + + while i < len(left): + arr[k] = left[i] + i += 1 + k += 1 + + while j < len(right): + arr[k] = right[j] + j += 1 + k += 1 # Code to print the list -def printList(arr): +def printList(arr): + for i in range(len(arr)): + print(arr[i], end = " ") + print() - #write your code here - +#write your code here # driver code to test the above code if __name__ == '__main__': arr = [12, 11, 13, 5, 6, 7] diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..bfef5f77 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,59 @@ # Python program for implementation of Quicksort +# Time Complexity :O(n*log(n)) +# Space Complexity :O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None +# Your code here along with comments explaining your approach: The code implements iterative QuickSort using an explicit stack instead of recursion. The partition function rearranges elements around a pivot (last element), placing smaller elements to the left and larger ones to the right, and returns the pivot’s final index. The quickSortIterative function pushes subarray bounds onto the stack, processes them in a loop, and sorts the array by repeatedly partitioning until the entire array is sorted. + + # This function is same in both iterative and recursive def partition(arr, l, h): #write your code here + i = l - 1 + x = arr[h] + + for j in range(l, h): + if arr[j] <= x: + i = 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 = [0] * (h - l + 1) + top = -1 + + top = top + 1 + stack[top] = l + top = top + 1 + stack[top] = h + + while top >= 0: + h = stack[top] + top = top -1 + l = stack[top] + top = top - 1 + + p = partition(arr, l, h) + + if p -1 > l: + top = top + 1 + stack[top] = l + top = top + 1 + stack[top] = p - 1 + if p + 1 < h: + top = top + 1 + stack[top] = p + 1 + top = top + 1 + stack[top] = h + +arr = [4, 3, 5, 2, 1, 3, 2, 3] +n = len(arr) +quickSortIterative(arr, 0, n-1) +print ("Sorted array is:") +for i in range(n): + print ("%d" %arr[i])