From a9c724de4ad21841a61186735fc3c6a3de5c1b04 Mon Sep 17 00:00:00 2001 From: nikhylw <1.nikhil.wani+nikhly@gmail.com> Date: Sat, 30 May 2026 00:44:14 +0530 Subject: [PATCH] adding pre-courese2 solutions --- Exercise_1.py | 49 +++++++++++++++++++++---------------- Exercise_2.py | 61 ++++++++++++++++++++++++++++----------------- Exercise_3.py | 68 +++++++++++++++++++++++++++++++-------------------- Exercise_4.py | 68 ++++++++++++++++++++++++++++++++++++++------------- Exercise_5.py | 57 +++++++++++++++++++++++++++++++++++++++--- 5 files changed, 213 insertions(+), 90 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..6276b184 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,29 @@ -# 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 - - - -# Test array -arr = [ 2, 3, 4, 10, 40 ] +# 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): + 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 - -# Function call -result = binarySearch(arr, 0, len(arr)-1, x) - -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) + +if result != -1: + print("Element is present at index %d" % result) +else: + print("Element is not present in array") \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..33dbb3c1 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,38 @@ -# Python program for implementation of Quicksort Sort - -# give you explanation for the approach -def partition(arr,low,high): - - - #write your code here - - -# Function to do Quick sort -def quickSort(arr,low,high): - - #write your code here - -# 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]), - - +# Python program for implementation of Quick Sort + +# Approach: +# Pick the last element as pivot. +# Place all smaller elements before pivot and greater elements after pivot. +# Then recursively sort the left and right parts. + +def partition(arr, low, high): + 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): + if low < high: + pi = partition(arr, low, high) + + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 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], end=" ") \ No newline at end of file diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..3808aabf 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,26 +1,42 @@ -# Node class -class Node: - - # Function to initialise the node object - def __init__(self, data): - -class LinkedList: - - def __init__(self): - - - def push(self, new_data): - - - # Function to get the middle of - # the linked list - def printMiddle(self): - -# Driver code -list1 = LinkedList() -list1.push(5) -list1.push(4) -list1.push(2) -list1.push(3) -list1.push(1) -list1.printMiddle() +# Node class +class Node: + + # Function to initialise the node object + def __init__(self, data): + self.data = data + self.next = None + + +class LinkedList: + + 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_ptr = self.head + fast_ptr = self.head + + if self.head is not None: + while fast_ptr is not None and fast_ptr.next is not None: + fast_ptr = fast_ptr.next.next + slow_ptr = slow_ptr.next + + print("The middle element is:", slow_ptr.data) + + +# Driver code +list1 = LinkedList() +list1.push(5) +list1.push(4) +list1.push(2) +list1.push(3) +list1.push(1) + +list1.printMiddle() \ No newline at end of file diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..2f527d71 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,18 +1,52 @@ -# Python program for implementation of MergeSort +# Python program for implementation of MergeSort + def mergeSort(arr): - - #write your code here - -# Code to print the list -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") - printList(arr) - mergeSort(arr) - print("Sorted array is: ", end="\n") - printList(arr) + 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): + for i in range(len(arr)): + print(arr[i], end=" ") + print() + + +# driver code to test the above code +if __name__ == '__main__': + arr = [12, 11, 13, 5, 6, 7] + + print("Given array is", end="\n") + printList(arr) + + mergeSort(arr) + + print("Sorted array is: ", end="\n") + printList(arr) \ No newline at end of file diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..286826b8 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,61 @@ -# Python program for implementation of Quicksort +# Python program for implementation of Iterative Quicksort # This function is same in both iterative and recursive def partition(arr, l, h): - #write your code here + pivot = arr[h] + i = l - 1 + + for j in range(l, h): + if arr[j] <= pivot: + 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 + size = h - l + 1 + stack = [0] * size + + top = -1 + + top += 1 + stack[top] = l + + top += 1 + stack[top] = h + + while top >= 0: + h = stack[top] + top -= 1 + + l = stack[top] + top -= 1 + + p = partition(arr, l, h) + + if p - 1 > l: + top += 1 + stack[top] = l + + top += 1 + stack[top] = p - 1 + + if p + 1 < h: + top += 1 + stack[top] = p + 1 + + top += 1 + stack[top] = h + + +# 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("%d" % arr[i], end=" ") \ No newline at end of file