From 0b2b2d7a92f0f0e7ae8f25ffe410831ca47dd99d Mon Sep 17 00:00:00 2001 From: prenastro Date: Sun, 28 Jun 2026 02:57:08 -0700 Subject: [PATCH 1/2] Completed S30 pre-course2 --- Exercise_1.py | 26 +++++++++++++++++++------- Exercise_2.py | 28 ++++++++++++++++++++++++---- Exercise_3.py | 32 ++++++++++++++++++++++++++++---- Exercise_4.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- Exercise_5.py | 34 ++++++++++++++++++++++++++++++++-- test.py | 6 ++++++ 6 files changed, 155 insertions(+), 19 deletions(-) create mode 100644 test.py diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..7823a4a8 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -4,11 +4,19 @@ # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - - #write your code here - - - + + #write your code here + + while l<=r: + m = (l + r)// 2 + if arr[m]>x: + r = m-1 + elif arr[m] 1: + mid = len(arr) // 2 + + # Divide + left = arr[:mid] + right = arr[mid:] + + # Recursively sort both halves + mergeSort(left) + mergeSort(right) + + # Merge the two sorted halves + i = j = k = 0 + + # Compare elements from left and right + 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 + + # Copy remaining elements of left + while i < len(left): + arr[k] = left[i] + i += 1 + k += 1 + + # Copy remaining elements of right + while j < len(right): + arr[k] = right[j] + j += 1 + k += 1 - #write your code here + # Code to print the list def printList(arr): #write your code here - + for x in arr: + print(x, end=" ") + print() + + # driver code to test the above code if __name__ == '__main__': arr = [12, 11, 13, 5, 6, 7] @@ -16,3 +57,6 @@ def printList(arr): mergeSort(arr) print("Sorted array is: ", end="\n") printList(arr) + +# TC - O(nlogn) +# SC - O(n) \ No newline at end of file diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..7e900338 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -2,9 +2,39 @@ # This function is same in both iterative and recursive def partition(arr, l, h): - #write your code here + #write your code here + pivot = arr[h] + i = l + + for j in range(l, h): + if arr[j] <= pivot: + arr[i], arr[j] = arr[j], arr[i] + i += 1 + + arr[i], arr[h] = arr[h], arr[i] + return i def quickSortIterative(arr, l, h): - #write your code here + # write your code here + stack = [(l, h)] + + while stack: + l, h = stack.pop() + + if l >= h: + continue + + p = partition(arr, l, h) + + stack.append((l, p - 1)) + stack.append((p + 1, h)) + + +# Driver code (NO INDENTATION) +arr = [10, 7, 8, 9, 1, 5] +quickSortIterative(arr, 0, len(arr) - 1) +print(arr) +# TC - O(nlogn) +# SC - O(logn) - average O(n^2) worst case \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 00000000..cfd73b66 --- /dev/null +++ b/test.py @@ -0,0 +1,6 @@ +def getMinimumTime(requestedHubs, transitionTime): + m = len(requestedHubs) + + pre = [0] * (m+1) + for i in range(1, m+1): + pre[i] = pre[i-1] + requestedHubs[i] From ea29689796bd2de0a77bac0c8a2be4b1bbecab43 Mon Sep 17 00:00:00 2001 From: Preranathm Date: Sun, 28 Jun 2026 02:59:57 -0700 Subject: [PATCH 2/2] Delete unused file --- test.py | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index cfd73b66..00000000 --- a/test.py +++ /dev/null @@ -1,6 +0,0 @@ -def getMinimumTime(requestedHubs, transitionTime): - m = len(requestedHubs) - - pre = [0] * (m+1) - for i in range(1, m+1): - pre[i] = pre[i-1] + requestedHubs[i]