diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..dd079c8b 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,31 @@ -# 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): + + # write your code here + while l <= r: + m = l + (r - l) // 2 + if arr[m] == x: + return m + elif arr[m] > x: + r = m - 1 + else: + l = m + 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:", result) +else: + print("Element is not present in array") diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..b2bcf7fb 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,35 @@ -# Python program for implementation of Quicksort Sort - +# 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): + # write your code here + pivot = arr[high] + + start = low + for end in range(low, high): + if arr[end] <= pivot: + arr[start], arr[end] = arr[end], arr[start] + start+=1 + arr[start], arr[high] = arr[high], arr[start] + return start + + +# Function to do Quick sort +def quickSort(arr, low, high): + # write your code here + if low < high: + index = partition(arr, low, high) + # Sort left half recursively + quickSort(arr, low, index - 1) + # Sort right half recursively + quickSort(arr, index + 1, high) + -# 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]), - - +# 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..72404185 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,26 +1,38 @@ -# 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): + node = Node(new_data) + node.next = self.head + self.head = node + + # Function to get the middle of + # the linked list + def printMiddle(self): + slow = self.head + fast = self.head + while fast != None and fast.next != None: + slow = slow.next + fast = fast.next.next + print(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..0a00ebb8 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,10 +1,35 @@ # Python program for implementation of MergeSort def mergeSort(arr): + if len(arr)<=1: + return arr + mid=len(arr)//2 + left = mergeSort(arr[:mid]) + right = mergeSort(arr[mid:]) + + return merge(left,right) #write your code here +def merge(left,right): + result=[] + i=0 + j=0 + + while il: + callstack.append((l,pivot-1)) + if pivot+1