diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..f46abe6b 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,30 @@ -# 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 ] +# Time complexity: O(log n) +# Space complexity: O(1) + +# 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) // 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") diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..80dc5659 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,40 @@ -# 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]), - - + +# Time complexity: O(n log n) +# Space complexity: O(log n) + +# The approach is to use the partition function to place the pivot in its correct position, +# and then recursively sort the sub-arrays on either side of the pivot. +def partition(arr, low, high): + # Select the rightmost element as pivot + pivot = arr[high] + # Index of smaller element (initially -1) + i = -1 + + for j in range(low, high): + # If current element is smaller than or equal to pivot + if arr[j] <= pivot: + i = i + 1 + arr[i], arr[j] = arr[j], arr[i] + + # Place pivot in its correct position + 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: + # Partition the array + 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] +n = len(arr) +quickSort(arr, 0, n-1) +print("Sorted array is:") +for i in range(n): + print("%d" % arr[i]), diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..e947a307 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,26 +1,41 @@ -# 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() +# Time complexity: O(n) +# Space complexity: O(1) + +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 = 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) +list1.push(4) +list1.push(2) +list1.push(3) +list1.push(1) +list1.printMiddle() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..44965028 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,12 +1,42 @@ +# Time complexity: O(n log n) +# Space complexity: O(n) + # Python program for implementation of MergeSort 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): - - #write your code here + for i in range(len(arr)): + print(arr[i], end=" ") + print() # driver code to test the above code if __name__ == '__main__': diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..8cb64dc1 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,37 @@ +# Time complexity: O(n log n) +# Space complexity: O(log n) + # Python program for implementation of Quicksort +def partition(arr, low, high): + # Select the rightmost element as pivot + pivot = arr[high] + + # Index of the smaller element within the current subarray + i = low - 1 + + for j in range(low, high): + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] + + # Place the pivot in its correct position + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 + + +def quick_sort_iterative(arr, low, high): + if low >= high: + return -# This function is same in both iterative and recursive -def partition(arr, l, h): - #write your code here + stack = [(low, high)] + while stack: + low, high = stack.pop() + pivot_index = partition(arr, low, high) -def quickSortIterative(arr, l, h): - #write your code here + # Push subarrays containing at least two elements + if low < pivot_index - 1: + stack.append((low, pivot_index - 1)) + if pivot_index + 1 < high: + stack.append((pivot_index + 1, high)) \ No newline at end of file