diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..6b0871ee 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -4,8 +4,15 @@ # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - - #write your code here + while l <= r: + m = l + (r - l)//2 + if arr[m] == x: + return m + if x < arr[m]: + r = m - 1 + else: + l = m + 1 + return -1 @@ -17,6 +24,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",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..cd0c3b0b 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,16 +1,30 @@ # Python program for implementation of Quicksort Sort # give you explanation for the approach -def partition(arr,low,high): - - - #write your code here - +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): - - #write your code here + # base condition + if low >= high: + return + + # partition the array + pi = partition(arr, low, high) + + # recursively sort left and right parts + 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..00c8638f 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,31 @@ # Node class -class Node: - +class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data,next=None): + self.data = data + self.next = next class LinkedList: def __init__(self): - + self.head = Node(-1) - def push(self, new_data): + def push(self, new_data): + temp = self.head + while temp.next: + temp = temp.next + newNode = Node(new_data) + temp.next = newNode # Function to get the middle of # the linked list - def printMiddle(self): + def printMiddle(self): + slow,fast = self.head.next,self.head.next + while fast and fast.next: + slow = slow.next + fast = fast.next.next + print(slow.data) # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..47a12a22 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,12 +1,34 @@ # 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 +def printList(arr): + for item in arr: + print(item) + # driver code to test the above code if __name__ == '__main__': diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..84dee71c 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 += 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 + # Create an explicit stack + stack = [] + + # push initial range + stack.append((l, h)) + + # Process stack until empty + while stack: + l, h = stack.pop() + + # Partition the array + pi = partition(arr, l, h) + + # If elements on left side of pivot, push left side + if pi - 1 > l: + stack.append((l, pi - 1)) + + # If elements on right side of pivot, push right side + if pi + 1 < h: + stack.append((pi + 1, h))