From c8e70b707dbd334abe2642c86cb2a6fce9b4954e Mon Sep 17 00:00:00 2001 From: Vansh Maheshbhai Virani <124015720+vvirani1908@users.noreply.github.com> Date: Sun, 17 Aug 2025 17:06:37 -0700 Subject: [PATCH] Done --- Exercise_1.py | 33 +++++++++++++++++++++++++++---- Exercise_2.py | 43 +++++++++++++++++++++++++++++++++------- Exercise_3.py | 33 ++++++++++++++++++++++++++----- Exercise_4.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- Exercise_5.py | 37 ++++++++++++++++++++++++++++++++++- 5 files changed, 179 insertions(+), 21 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..84e514e5 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,11 +1,36 @@ -# Python code to implement iterative Binary +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# Approach: +# 1. Initialize two pointers, l (left) and r (right), to mark the search boundaries. +# 2. While l <= r: +# - Compute the middle index m = (l + r) // 2. +# - If nums[m] is greater than the target, move r to m - 1 (search left half). +# - If nums[m] is less than the target, move l to m + 1 (search right half). +# - If nums[m] equals the target, return m (element found). +# 3. If the loop ends without returning, the element is not present → return -1. + +# 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 + l, r = 0, len(arr)-1 + while l <= r: + m = (l + r) // 2 + if arr[m] > x: + r = m - 1 + elif arr[m] < x: + l = m + 1 + else: + return m + + return -1 @@ -17,6 +42,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..bfe936b7 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,16 +1,45 @@ -# Python program for implementation of Quicksort Sort +# Time Complexity : Average O(n log n), Worst O(n^2) if pivot choices are poor +# Space Complexity : O(log n) due to recursion stack (in-place sort) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +# Approach: +# 1. Choose a pivot (here we choose the last element in the current subarray). +# 2. Partition the array into two halves: +# - All elements <= pivot move to the left side. +# - All elements > pivot move to the right side. +# This is done using a pointer `i` to track the position where the next smaller element should go. +# 3. Swap the pivot into its correct sorted position. +# 4. Recursively apply the same logic to the left and right subarrays (excluding the pivot). +# 5. Sorting happens in-place, requiring no extra array. + +# Python program for implementation of Quicksort Sort # give you explanation for the approach def partition(arr,low,high): - - - #write your code here + pivot = arr[high] # choosing last element as pivot + i = low - 1 # index of smaller element + + for j in range(low, high): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] # swap + + arr[i + 1], arr[high] = arr[high], arr[i + 1] # place pivot in correct position + return i + 1 + # Function to do Quick sort -def quickSort(arr,low,high): - - #write your code here +def quickSort(arr,low,high): + if low < high: + 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] diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..a131a660 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,43 @@ -# Node class +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# 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 __init__(self): + self.head = None - def push(self, new_data): + 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): + slow_ptr = self.head + fast_ptr = self.head + + while fast_ptr is not None and fast_ptr.next is not None: + fast_ptr = fast_ptr.next.next + slow_ptr = slow_ptr.next + + # slow_ptr will now be at the middle + if slow_ptr: + print("The middle element is:", slow_ptr.data) + else: + print("The list is empty") + # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..82177506 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,10 +1,56 @@ -# 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 : No + +# Python program for implementation of MergeSort 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 + L = arr[:mid] + R = arr[mid:] + + # Recursive call to sort both halves + mergeSort(L) + mergeSort(R) + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + 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 + + # Checking if any element was left + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 + + +#write your code here # 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 diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..f85d50bc 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,45 @@ +# Time Complexity : O(n log n) +# Space Complexity : O(log n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No # Python program for implementation of Quicksort # This function is same in both iterative and recursive def partition(arr, l, h): - #write your code here + pivot = arr[h] # take last element as pivot + i = l - 1 # index of smaller element + + for j in range(l, h): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] # swap + + arr[i + 1], arr[h] = arr[h], arr[i + 1] # place pivot in correct position + return i + 1 + + +#write your code here def quickSortIterative(arr, l, h): + stack = [] + + # Push initial values of l and h + stack.append((l, h)) + + # Keep popping until stack is empty + while stack: + l, h = stack.pop() + if l < h: + # Partition the array + p = partition(arr, l, h) + + # If elements exist on left side of pivot, push left side to stack + if p - 1 > l: + stack.append((l, p - 1)) + + # If elements exist on right side of pivot, push right side to stack + if p + 1 < h: + stack.append((p + 1, h)) #write your code here