diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..90e189ea 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -5,7 +5,17 @@ # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here + while l <= r: + mid = (l+r)//2 + + if arr[mid] == x: + return mid + elif arr[mid] > x: + r = mid - 1 + elif arr[mid] < x: + l = mid+1 + + return -1 @@ -17,6 +27,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 % 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..2e187926 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,16 +1,53 @@ +# // Time Complexity : Best/Average: O(nlogn), Worst O(n^2) depending on pivot. n^2 if pivot is first or last element +# // Space Complexity : O(n) +# // Did this code successfully run on Leetcode : +# // Any problem you faced while coding this : + + +# // Your code here along with comments explaining your approach + + + # 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 is the item that we need to find a position for + pivot = arr[low] + # 'left' will track the position where elements smaller than pivot go + left = low + + for i in range(low + 1, high + 1): # high + 1 to include the last element + if arr[i] < pivot: + left += 1 + # Swap current element with the leftmost available slot + arr[i], arr[left] = arr[left], arr[i] -# Function to do Quick sort -def quickSort(arr,low,high): + # Finally, move the pivot from the start to its correct sorted position + arr[low], arr[left] = arr[left], arr[low] - #write your code here + return left + +# PIVOT - THE ITEM I WANT TO FIND A POSITION FOR +def quickSort(arr, low, high): + if low < high: + pivotLocation = partition(arr, low, high) # returns pivot index + # Sort elements before and after the pivot + quickSort(arr, low, pivotLocation - 1) + quickSort(arr, pivotLocation + 1, high) + +# [1, 5, 8, 9, 7, 10] +# l i +# quicksort(arr,0,5) +# pivotLocation = partition(arr,0,5) +# pivot = arr[0] -> 10 +# left = 0 +# for i = 1->5 +# pivotLocation = 5 +# quickSort(arr, 0, 4) +# partition(arr,0,4) +# quickSort(arr, 6, 5) + # Driver code to test above arr = [10, 7, 8, 9, 1, 5] diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..85777cb5 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,20 +1,45 @@ +# // Time Complexity : O(n) +# // Space Complexity : O(n) +# // Did this code successfully run on Leetcode : +# // Any problem you faced while coding this : + + # Node class 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 = None def push(self, new_data): - + newNode = Node(new_data) + if self.head is None: + self.head = newNode + else: + # 1->2 + cur = self.head + while cur.next: + cur = cur.next + + cur.next = newNode + # 1->2->3->4->5 + # t h # Function to get the middle of # the linked list def printMiddle(self): + tort = self.head + hare = self.head + while hare and hare.next: + hare = hare.next.next + tort = tort.next + + return tort # Driver code list1 = LinkedList() diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..98af8847 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,8 +1,42 @@ # Python program for implementation of MergeSort + + +# [12, 11, 13, 5, 6, 7] 6 +# left = [12, 11, 13] +# right= [5, 6, 7] def mergeSort(arr): - #write your code here - + if len(nums) <= 1: + return nums + + mid = len(nums) // 2 + + # left = [12, 11, 13] + left = mergeSort(nums[:mid]) + # right= [5, 6, 7] + right= mergeSort(nums[mid:]) + + return merge(left, right) + +# res = [11,12,13] +# left = [12] +# l +# right =[11,13] +# r +def merge(left,right): + res = [] + l=r=0 + + while l < len(left) and r < len(right): + if left[l] < right[r]: + res.append(left[l]) + l+=1 + else: + res.append(right[r]) + r+=1 + + res.extend(left[l:]) + res.extend(right[r:]) # Code to print the list def printList(arr): diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..6c4d3889 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,40 @@ # Python program for implementation of Quicksort # This function is same in both iterative and recursive -def partition(arr, l, h): +def partition(arr, low, high): #write your code here + pivot = arr[low] + left = low + for i in range(low+1,high+1): + if arr[i] < pivot: + left+=1 + arr[i],arr[left] = arr[left], arr[i] + arr[low],arr[left] = arr[left],arr[low] + return left -def quickSortIterative(arr, l, h): + +def quickSortIterative(arr): #write your code here + size = len(arr) + stack = [(0,size-1)] + + while stack: + low,high = stack.pop() + + p = partition(arr,low,high) + + if p-1 > low: + stack.append((low,p-1)) + + if p+1 < high: + stack.append((p+1,high)) + +arr = [10, 7, 8, 9, 1, 5] +quickSortIterative(arr) +print("Sorted array:", arr) + +# stack = [(0,5)] +# low = 0, high = 5 +# p = partition(arr,0,5) \ No newline at end of file