From b35ceb83c60c73e7ffa25635deba4fb5ee00dea7 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sun, 9 Nov 2025 14:21:56 -0500 Subject: [PATCH 01/12] exercise-1 binary sort --- Exercise_1.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..0fe98987 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -5,18 +5,37 @@ # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here + # write your code here + # sorting the given array + arr.sort() + + # finding the middle index + mid_index = int((l + r) / 2) + print("func called with", l,r) + # checking if middle is equal to given element + if x == arr[mid_index]: + return mid_index + # checking if the given element is grater than middle element + elif (x > arr[mid_index]): + print("x > mid_index") + return binarySearch(arr, mid_index+1, r, x) + # checking if the given element is less than middle element + elif (x < arr[mid_index]): + print("x < mid_index") + return binarySearch(arr,l ,mid_index-1, x) + + return -1 # Test array -arr = [ 2, 3, 4, 10, 40 ] -x = 10 +arr = [ 2, 3, 4, 10, 40] +x = 3 # Function call 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 ", result) else: - print "Element is not present in array" + print("Element is not present in array") From d96c358b202bda73606c5393b617743249861f79 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sun, 9 Nov 2025 14:24:30 -0500 Subject: [PATCH 02/12] exercise-1 binary sort --- Exercise_1.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 0fe98987..d56279ad 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -11,19 +11,16 @@ def binarySearch(arr, l, r, x): # finding the middle index mid_index = int((l + r) / 2) - print("func called with", l,r) # checking if middle is equal to given element if x == arr[mid_index]: return mid_index # checking if the given element is grater than middle element elif (x > arr[mid_index]): - print("x > mid_index") return binarySearch(arr, mid_index+1, r, x) # checking if the given element is less than middle element elif (x < arr[mid_index]): - print("x < mid_index") return binarySearch(arr,l ,mid_index-1, x) return -1 From b75221d300ef0d2026b6b0c4764648a24fb59f9b Mon Sep 17 00:00:00 2001 From: Anushri Date: Sun, 9 Nov 2025 17:45:46 -0500 Subject: [PATCH 03/12] Exercise_2 quick sort --- Exercise_1.py | 2 +- Exercise_2.py | 45 ++++++++++++++++++++++++++++++++++++++++----- Exercise_4.py | 5 ++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index d56279ad..84c79968 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -33,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 ", result) + print("Element is present at index", result) else: print("Element is not present in array") diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..e4631460 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,58 @@ # Python program for implementation of Quicksort Sort # give you explanation for the approach + +# in the parition function, finding the partition position of the pivot element def partition(arr,low,high): - - #write your code here - + # taking first element as pivot means find the sorted position of this pivot element + pivot_index = low + pivot_element = arr[pivot_index] + + # start with the next element of the pivot to start comparing + i = pivot_index + 1 + j = high + + # find the position of pivot. until the low and high intersect, keep going + while i <= j: + + # find the next element larger than pivot so i = pivot_index + 1 + # keep incrementing low until we find an element that is larger than pivot + while i <= j and arr[i] <= pivot_element: + i += 1 + # find the next element smaller than pivot + # keep decrementing high until we find an element that is smaller than pivot + while i <= j and arr[j] >= pivot_element: + j -= 1 + if i <= j: + # swapping these elements + arr[i], arr[j] = arr[j], arr[i] + + # place the pivot element on its sorted position(j is the sorted position because any element after j is greater + # and any element before j is smaller) and that positional element on the very first + arr[pivot_index], arr[j] = arr[j], arr[pivot_index] + + # returning the sorted element position which is our partition + return j # Function to do Quick sort def quickSort(arr,low,high): #write your code here - + if low < high: + # taken the sorted element position in the variable + partition_index = partition(arr,low,high) + # perform quicksort on LHS + quickSort(arr, low, partition_index) + #perform quicksort on RHS + quickSort(arr, partition_index+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:") for i in range(n): - print ("%d" %arr[i]), + print("i",arr[i]) diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..a71d52de 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -9,10 +9,9 @@ def printList(arr): #write your code here # driver code to test the above code -if __name__ == '__main__': arr = [12, 11, 13, 5, 6, 7] - print ("Given array is", end="\n") + print("Given array is", arr) printList(arr) mergeSort(arr) - print("Sorted array is: ", end="\n") + print("Sorted array is: ",) printList(arr) From 4be3bad23d5c404238ac6f419562e740da5304d2 Mon Sep 17 00:00:00 2001 From: Anushri Date: Sun, 9 Nov 2025 17:51:34 -0500 Subject: [PATCH 04/12] Exercise_1 binary sort --- Exercise_1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 84c79968..fd9167c6 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,6 +1,6 @@ # 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): @@ -35,4 +35,4 @@ def binarySearch(arr, l, r, x): if result != -1: print("Element is present at index", result) else: - print("Element is not present in array") + print("Element is not present in array") \ No newline at end of file From 24a914f959cdab92dba270d46db170dda3fd251f Mon Sep 17 00:00:00 2001 From: Anushri Date: Sun, 9 Nov 2025 21:37:13 -0500 Subject: [PATCH 05/12] Exercise_3 mid point of a singly linked list --- Exercise_3.py | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..96fac1e7 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,26 +1,53 @@ +import math # Node class class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data = None, next = None): + self.data = data + self.next = next class LinkedList: - def __init__(self): - + def __init__(self): + # initializing head with none + self.head = None def push(self, new_data): - - - # Function to get the middle of - # the linked list + # creating an object + next_node = Node(new_data) + if self.head: + node = self.head + while node.next: + node = node.next + node.next = next_node + else: + self.head = next_node + + # Function to get the middle of the linked list def printMiddle(self): + length = 0 + if self.head: + node = self.head + while node.next: + length += 1 + node = node.next + # find the mid_index of the linked list + mid_index = math.ceil(length / 2) + node = self.head + # interate over the found mid_index + for _index in range(mid_index): + node = node.next + return node.data + return None + # Driver code list1 = LinkedList() + list1.push(5) list1.push(4) list1.push(2) list1.push(3) -list1.push(1) -list1.printMiddle() +list1.push(1) +print(list1.printMiddle()) From b25febbdb02c8666cb61bf2e559a066f5928fe4f Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 09:43:39 -0500 Subject: [PATCH 06/12] Exercise_4 merge sort --- Exercise_4.py | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/Exercise_4.py b/Exercise_4.py index a71d52de..4e2dde07 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -2,16 +2,50 @@ def mergeSort(arr): #write your code here - + # dividing the array + if len(arr) > 1: + mid = len(arr) // 2 + lhs = arr[0: mid] + rhs = arr[mid:] + mergeSort(lhs) + mergeSort(rhs) + + # merging the array + i, j = 0,0 + k = 0 + # merge_list =[] + while i < len(lhs) and j < len(rhs): + if lhs[i] < rhs[j]: + arr[k] = lhs[i] + # merge_list.append(lhs[i]) + i += 1 + else: + arr[k] = rhs[j] + # merge_list.append(rhs[j]) + j +=1 + k += 1 + while i < len(lhs): + arr[k] = lhs[i] + # merge_list.append(lhs[i]) + i += 1 + k += 1 + while j < len(rhs): + arr[k] = rhs[j] + # merge_list.append(rhs[j]) + j +=1 + k +=1 + + # Code to print the list def printList(arr): #write your code here - + print(arr) + # driver code to test the above code - arr = [12, 11, 13, 5, 6, 7] - print("Given array is", arr) - printList(arr) - mergeSort(arr) - print("Sorted array is: ",) - printList(arr) +arr = [12, 11, 13, 5, 6, 7] +print("Given array is", arr) +printList(arr) +mergeSort(arr) +print("Sorted array is: ",arr) +printList(arr) From 11a28f12aba7a7547002a06cb9bfa2f8e4722da3 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 16:00:05 -0500 Subject: [PATCH 07/12] Exercise_2 Quick Sort --- Exercise_2.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Exercise_2.py b/Exercise_2.py index e4631460..5ea4e235 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,5 +1,9 @@ # Python program for implementation of Quicksort Sort - +# Time Complexity : O(n log n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# Any problem you faced while coding this : No + # give you explanation for the approach # in the parition function, finding the partition position of the pivot element From 74871462eec97a2bde8a5cf5fb26a4db740c0ccb Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 16:10:58 -0500 Subject: [PATCH 08/12] Exercise_1 Binary Search --- Exercise_1.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index fd9167c6..dbc6f270 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,5 +1,8 @@ -# Python code to implement iterative Binary -# Search. +# Python code to implement iterative Binary Search. +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : No. Could not find this problem(with 4 argument) on geeks or leetcode +# Any problem you faced while coding this : No # It returns location of x in given array arr # if present, else returns -1 @@ -26,7 +29,7 @@ def binarySearch(arr, l, r, x): return -1 # Test array -arr = [ 2, 3, 4, 10, 40] +arr = [2, 3, 4, 10, 40] x = 3 # Function call From 9c3fabb0fe7327908ebf24547b8cc8e4881e0809 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 16:39:24 -0500 Subject: [PATCH 09/12] Exercise_4 Merge Sort --- Exercise_4.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Exercise_4.py b/Exercise_4.py index 4e2dde07..e7c944e3 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,4 +1,8 @@ # Python program for implementation of MergeSort +# Time Complexity : O(n log n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : No. Could not find this problem(with 4 argument) on geeks or leetcode +# Any problem you faced while coding this : Yes. While implementing the merging logic. def mergeSort(arr): #write your code here @@ -7,28 +11,39 @@ def mergeSort(arr): mid = len(arr) // 2 lhs = arr[0: mid] rhs = arr[mid:] + # dividing the lhs of the array until only one element is left in the array mergeSort(lhs) + # dividing the rhs of the array until only one element is left in the array mergeSort(rhs) # merging the array + # index for lhs and rhs i, j = 0,0 + # index for the merged array k = 0 # merge_list =[] while i < len(lhs) and j < len(rhs): + # if the left side element is smaller than the right hand side, add into the array if lhs[i] < rhs[j]: arr[k] = lhs[i] # merge_list.append(lhs[i]) + # increment the left element i += 1 + # else add right side element into the array else: arr[k] = rhs[j] # merge_list.append(rhs[j]) + # increment the right element j +=1 + # increment the merged array to get the next element k += 1 + # merging the remaning left side element at the end of the list while i < len(lhs): arr[k] = lhs[i] # merge_list.append(lhs[i]) i += 1 k += 1 + # merging the remaning right side element at the end of the list while j < len(rhs): arr[k] = rhs[j] # merge_list.append(rhs[j]) From 1b4a34fac6aae9f19acc78cdf878c4e923b20e22 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 16:47:05 -0500 Subject: [PATCH 10/12] Exercise_4 Merge Sort --- Exercise_4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Exercise_4.py b/Exercise_4.py index e7c944e3..fc5101a4 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,7 +1,7 @@ # Python program for implementation of MergeSort # Time Complexity : O(n log n) # Space Complexity : O(n) -# Did this code successfully run on Leetcode : No. Could not find this problem(with 4 argument) on geeks or leetcode +# Did this code successfully run on Leetcode : No. Could not find this problem (with 1 argument) on geeks or leetcode # Any problem you faced while coding this : Yes. While implementing the merging logic. def mergeSort(arr): From 077a8063480a32b7a8f384f2bc45d4794d66b965 Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 16:48:14 -0500 Subject: [PATCH 11/12] Exercise_5 Iterative Quick Sort --- Exercise_5.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..bb3a7c61 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,61 @@ # Python program for implementation of Quicksort +# Time Complexity : O(log n) +# Space Complexity : O(log n) +# Did this code successfully run on Leetcode : No. Could not find this problem on geeks or leetcode +# Any problem you faced while coding this : Yes. While implementing the Iterative sort logic. # This function is same in both iterative and recursive def partition(arr, l, h): #write your code here + pivot_index = l + pivot_element = arr[pivot_index] + i = pivot_index + 1 + j = h + + while i <= j: + while i <=j and arr[i] <= pivot_element: + i += 1 + + while i <=j and arr[j] >= pivot_element: + j -= 1 + + if i <=j: + arr[i], arr[j] = arr[j], arr[i] + + arr[pivot_index], arr[j] = arr[j], arr[pivot_index] + + return j def quickSortIterative(arr, l, h): #write your code here + stack = [] + + # append starting range to stack + stack.append((l, h)) + + # continue this operation until the stack is empty means all the elements are considered + while stack: + l, h = stack.pop() + + if l < h: + # call the partition function and find the pivot element position(index) + p = partition(arr, l, h) + + # get the left hand side element from the pivot element position(index) + if p > l: + stack.append((l, p)) + + # get the right hand side element from the pivot element position + if p + 1 < h: + stack.append((p + 1, h)) + return arr + +# Driver code to test above +arr = [10, 7, 8, 9, 1, 5] +n = len(arr) +quickSortIterative(arr,0,n-1) +print ("Sorted array is:") +for i in range(n): + print("i",arr[i]) \ No newline at end of file From 5aeab2fc6a95359bddc683bb187ede53f91ca7bf Mon Sep 17 00:00:00 2001 From: Anushri Date: Mon, 10 Nov 2025 17:04:52 -0500 Subject: [PATCH 12/12] Exercise_3 Find Mid Point of a Singly Linked List --- Exercise_3.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Exercise_3.py b/Exercise_3.py index 96fac1e7..170227f6 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,3 +1,8 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes. On Geeksforgeeks +# Any problem you faced while coding this : No + import math # Node class class Node: