diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..a1a14ec1 Binary files /dev/null and b/.DS_Store differ diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..1dc71cc9 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -3,20 +3,24 @@ # It returns location of x in given array arr # if present, else returns -1 -def binarySearch(arr, l, r, x): +def binarySearch(arr, left, right, x): + while left <= right: + mid = (left + right) // 2 + if arr[mid] == x: + return mid + elif arr[mid] > x: + right = mid - 1 + else: + left = mid + 1 + return -1 + + +arr = [2, 3, 4, 10, 40] +x = 50 - #write your code here - - - -# Test array -arr = [ 2, 3, 4, 10, 40 ] -x = 10 - -# Function call -result = binarySearch(arr, 0, len(arr)-1, 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..be16e6f7 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -5,12 +5,29 @@ def partition(arr,low,high): #write your code here + pivot = arr[high] # last element as the pivot + i = low - 1 # smaller element + + for j in range(low, high): + # if the current element is smaller than or equal to the chosen pivot + if arr[j] <= pivot: + i += 1 + arr[i], arr[j] = arr[j], arr[i] # swapping elements + + # swapping the pivot element with the element at i+1 + 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..18446bc3 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,18 +3,34 @@ 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_ptr = self.head + fast_ptr = self.head + + while fast_ptr and fast_ptr.next: + slow_ptr = slow_ptr.next + fast_ptr = fast_ptr.next.next + + if slow_ptr: + print(f"The middle element is {slow_ptr.data}") + else: + print("The linked list is empty") # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..6798f4ff 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -2,11 +2,51 @@ def mergeSort(arr): #write your code here + if len(arr) > 1: + # the middle of the array + mid = len(arr) // 2 + + # dividing the array into left and right halves + left = arr[:mid] + right = arr[mid:] + + # recursively sorting the two halves + mergeSort(left) + mergeSort(right) + + # merging the sorted halves + i = j = k = 0 + + # copying data to temporary arrays left[] and right[] + 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 + + # checking for any remaining elements in left[] + while i < len(left): + arr[k] = left[i] + i += 1 + k += 1 + + # checking for any remaining elements in right[] + 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 arr: + print(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..1a084cad 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -3,8 +3,34 @@ # 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 + stack = [] + + stack.append((l, h)) + + while stack: + l, h = stack.pop() + + pivot_index = partition(arr, l, h) + + if pivot_index - 1 > l: + stack.append((l, pivot_index - 1)) + + if pivot_index + 1 < h: + stack.append((pivot_index + 1, h))