diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..6038b1c0 Binary files /dev/null and b/.DS_Store differ diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..42bd9c8b 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -3,20 +3,35 @@ # It returns location of x in given array arr # if present, else returns -1 + +#TC of binary search is O(log n) since we halve the work to be done at each division +#SC is also o(log n) as we only use the part that potentially has the element def binarySearch(arr, l, r, x): #write your code here - - + n = len(arr) + l = 0 + r = n - 1 + + while l <= r: + m = (l + r) // 2 + if arr[m] == x: + return m + elif arr[m] = p: #shifts all the elements greater than pivot to right + j -= 1 + if i <= j: + arr[i], arr[j] = arr[j], arr[i] #shifting done here + else: + break + arr[low], arr[j] = arr[j], arr[low] #swaping of pivot and the jth element, point where pivot is correctly placed + return j - - #write your code here - - # Function to do Quick sort def quickSort(arr,low,high): - + if low < high: + pivot = partition(arr, low, high) #calls for the position of pivot + quickSort(arr, low, pivot - 1) #recusrive call ofor left + quickSort(arr, pivot + 1, high) #recusrive call on right + #write your code here # Driver code to test above diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..8f52f0da 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,33 @@ # Node class +#TC is o(n) since the fast pointer moves thorugh all the elements +#SC is o(N) for pushing, and maybe constant for the finding part, since we use only two pointers 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): - + newNode = Node(new_data) + newNode.next = self.head #push the earlier head as the next element of the new node to be added + self.head = newNode #eastablish the new node as head # Function to get the middle of # the linked list def printMiddle(self): + slow, fast = self.head, self.head #slow and fast pointer, bcz fast jumps twice as compared to slow, when the fast will be at the end, the slow will be in the mid + while fast and fast.next: + slow = slow.next + fast = fast.next.next + return slow + # Driver code list1 = LinkedList() @@ -23,4 +36,4 @@ def printMiddle(self): list1.push(2) list1.push(3) list1.push(1) -list1.printMiddle() +print("Middle element:", list1.printMiddle().data) diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..26fcadd7 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,10 +1,46 @@ # Python program for implementation of MergeSort + +#TC for mergesort in any condition is O(nlogn) +#SC is o(n) def mergeSort(arr): + if len(arr) <= 1: + return arr + + mid = (len(arr)) //2 + left = arr[:mid] + right = arr[mid:] + lhalf = mergeSort(left) + rhalf = mergeSort(right) + return merge(lhalf, rhalf) + +def merge(left, right): + res =[] + i, j = 0, 0 + while i < len(left) and j < len(right): + if left[i] < right[j]: + + res.append(left[i]) + i += 1 + else: + res.append(right[j]) + j += 1 + res.extend(left[i:]) + res.extend(right[j:]) + return res + + + #write your code here # Code to print the list def printList(arr): + for i in arr: + print(i, end =" ") + print() + + + #write your code here @@ -13,6 +49,6 @@ def printList(arr): arr = [12, 11, 13, 5, 6, 7] print ("Given array is", end="\n") printList(arr) - mergeSort(arr) + arr = mergeSort(arr) print("Sorted array is: ", end="\n") printList(arr) diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..6ae624ae 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -2,9 +2,22 @@ # This function is same in both iterative and recursive def partition(arr, l, h): + p = arr[low] + i = low + 1 + j = high + while True: + while i <= j and arr[i] <= p: #shifts all the elements less than pivot to left + i += 1 + while i <= j and arr[j] >= p: #shifts all the elements greater than pivot to right + j -= 1 + if i <= j: + arr[i], arr[j] = arr[j], arr[i] #shifting done here + else: + break + arr[low], arr[j] = arr[j], arr[low] #swaping of pivot and the jth element, point where pivot is correctly placed + return j #write your code here def quickSortIterative(arr, l, h): - #write your code here - + #write your code he \ No newline at end of file