diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..f3cf81e7 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,35 @@ # Python code to implement iterative Binary -# Search. +# Search will use pointers left pointer will start at 0th index and right pointer will start at last index +# of the array and we use while loop to find the target element +# while condition will make sure the left and right pointer dont cross each other +# calcuate the middle and check if the middle index value is greater or smaller than the target +# if middle value is greater than the target then then point the right pointer to mid - 1 or else point left to mid + 1 # It returns location of x in given array arr # if present, else returns -1 -def binarySearch(arr, l, r, x): - - #write your code here - +def binarySearch(arr, l, r, x): + + while l <= r: + mid = (l + r) // 2 + + if arr[mid] == x: + return mid + elif arr[mid] > x: + r = mid - 1 + else: + l = mid + 1 + return -1 # Test array -arr = [ 2, 3, 4, 10, 40 ] -x = 10 +# arr = [ 2, 3, 4, 10, 40 ] +arr = [1, 2, 9] +x = 9 # 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 % 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..8d5ef272 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -2,15 +2,30 @@ # give you explanation for the approach 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 - #write your code here - # Function to do Quick sort -def quickSort(arr,low,high): +def quickSort(arr,low,high): + if low < high: + pi = partition(arr, low, high) + + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) - #write your code here # Driver code to test above arr = [10, 7, 8, 9, 1, 5] diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..3a515e85 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,48 @@ -# Node class -class Node: - +# Node class +""" +we use slow pointer and fast pointer to find the mid of the linkedlist +once the fast pointer reaches the tail then the slow pointer will be at the middle +""" +class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data): + self.data = data + self.next = None + -class LinkedList: - +class LinkedList: def __init__(self): - + self.head = None + self.tail = None - def push(self, new_data): + def push(self, new_data): + temp = Node(new_data) + + if self.head is None: + self.head = temp + self.tail = temp + return + + self.tail.next = temp + self.tail = temp # Function to get the middle of # the linked list - def printMiddle(self): + def printMiddle(self): + + if self.head is None: + return "List is empty" + + slow = self.head + fast = self.head + + while fast and fast.next: + slow = slow.next + fast = fast.next.next + + print(slow.data) + return slow # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..ba42916a 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,11 +1,57 @@ -# Python program for implementation of MergeSort +# Python program for implementation of MergeSort +""" +In merge sort we divide the array into half which will be the mid pointer +we use mid pointer to split the array into two half left and right +again we do the same until the length become 1 from the we will have pointer for i,j and k +i is left_half and j is for right_half +we compare the values of ith and jth element +we insert at the kth index to the original array +at the end we porcess the remaining length +incrementing the pointer at each step +time complexity O(n long n) +space comlexity O(n) +""" 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 + k +=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 a in arr: + print(a) #write your code here # driver code to test the above code diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..260dd4ca 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -4,7 +4,42 @@ 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 + stack = [(l, h)] + + while stack: + low, high = stack.pop() + + if low < high: + pi = partition(arr, low, high) + + if pi + 1 < high: + stack.append((pi + 1, high)) + + if low < pi - 1: + stack.append((low, pi - 1)) + + return arr + +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]), +