diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..aa7decc9 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -5,10 +5,24 @@ # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here - + while l <= r: + mid = l + (r - l) // 2 # Find middle element + + + 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 @@ -17,6 +31,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..0a358006 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -2,15 +2,21 @@ # give you explanation for the approach def partition(arr,low,high): - - - #write your code here - + pivot = arr[high] + i = low - 1 + for j in range(low, high): + if arr[j] <= pivot: + i = 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): - - #write your code here + 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] diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..ba507742 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -2,19 +2,32 @@ 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): - + 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 + + + while fast and fast.next: + fast = fast.next.next + slow = slow.next + + print("Middle element is:", slow.data) # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..ee368704 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,10 +1,43 @@ # Python program for implementation of MergeSort def mergeSort(arr): + if len(arr) > 1: + mid = len(arr) // 2 + left_half = arr[:mid] + right_half = arr[mid:] + + mergeSort(left_half) + mergeSort(right_half) + + i = j = k = 0 + + + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1 - #write your code here + # Code to print the list 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..8853c234 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -2,9 +2,37 @@ # 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 = 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 + stack = [] + + + stack.append((l, h)) + + + while stack: + l, h = stack.pop() + + + pi = partition(arr, l, h) + + + if pi - 1 > l: + stack.append((l, pi - 1)) + if pi + 1 < h: + stack.append((pi + 1, h)) +arr = [2, 1, 3, 5, 6, 7] +n = len(arr) +print("Given array is:", arr) +quickSortIterative(arr, 0, n - 1) +print("Sorted array is:", arr)