diff --git a/Exercise_1.py b/Exercise_1.py index 3e6adcf4..dbc6f270 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,41 @@ -# Python code to implement iterative Binary -# Search. - +# Python code to implement iterative Binary Search. +# Time Complexity : O(log n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : No. Could not find this problem(with 4 argument) on geeks or leetcode +# Any problem you faced while coding this : No + # It returns location of x in given array arr # if present, else returns -1 def binarySearch(arr, l, r, x): - #write your code here + # write your code here + # sorting the given array + arr.sort() + + # finding the middle index + mid_index = int((l + r) / 2) + # checking if middle is equal to given element + if x == arr[mid_index]: + return mid_index + # checking if the given element is grater than middle element + elif (x > arr[mid_index]): + return binarySearch(arr, mid_index+1, r, x) + # checking if the given element is less than middle element + elif (x < arr[mid_index]): + return binarySearch(arr,l ,mid_index-1, x) + + return -1 # Test array -arr = [ 2, 3, 4, 10, 40 ] -x = 10 +arr = [2, 3, 4, 10, 40] +x = 3 # 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", result) else: - print "Element is not present in array" + print("Element is not present in array") \ No newline at end of file diff --git a/Exercise_2.py b/Exercise_2.py index 35abf0dd..5ea4e235 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,23 +1,62 @@ # Python program for implementation of Quicksort Sort - +# Time Complexity : O(n log n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes on Geeksforgeeks +# Any problem you faced while coding this : No + # give you explanation for the approach + +# in the parition function, finding the partition position of the pivot element def partition(arr,low,high): - - #write your code here - + # taking first element as pivot means find the sorted position of this pivot element + pivot_index = low + pivot_element = arr[pivot_index] + + # start with the next element of the pivot to start comparing + i = pivot_index + 1 + j = high + + # find the position of pivot. until the low and high intersect, keep going + while i <= j: + + # find the next element larger than pivot so i = pivot_index + 1 + # keep incrementing low until we find an element that is larger than pivot + while i <= j and arr[i] <= pivot_element: + i += 1 + # find the next element smaller than pivot + # keep decrementing high until we find an element that is smaller than pivot + while i <= j and arr[j] >= pivot_element: + j -= 1 + if i <= j: + # swapping these elements + arr[i], arr[j] = arr[j], arr[i] + + # place the pivot element on its sorted position(j is the sorted position because any element after j is greater + # and any element before j is smaller) and that positional element on the very first + arr[pivot_index], arr[j] = arr[j], arr[pivot_index] + + # returning the sorted element position which is our partition + return j # Function to do Quick sort def quickSort(arr,low,high): #write your code here - + if low < high: + # taken the sorted element position in the variable + partition_index = partition(arr,low,high) + # perform quicksort on LHS + quickSort(arr, low, partition_index) + #perform quicksort on RHS + quickSort(arr, partition_index+1, high) + # 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]), + print("i",arr[i]) diff --git a/Exercise_3.py b/Exercise_3.py index a26a69b8..170227f6 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,26 +1,58 @@ +# Time Complexity : O(n) +# Space Complexity : O(1) +# Did this code successfully run on Leetcode : Yes. On Geeksforgeeks +# Any problem you faced while coding this : No + +import math # Node class class Node: # Function to initialise the node object - def __init__(self, data): + def __init__(self, data = None, next = None): + self.data = data + self.next = next class LinkedList: - def __init__(self): - + def __init__(self): + # initializing head with none + self.head = None def push(self, new_data): - - - # Function to get the middle of - # the linked list + # creating an object + next_node = Node(new_data) + if self.head: + node = self.head + while node.next: + node = node.next + node.next = next_node + else: + self.head = next_node + + # Function to get the middle of the linked list def printMiddle(self): + length = 0 + if self.head: + node = self.head + while node.next: + length += 1 + node = node.next + # find the mid_index of the linked list + mid_index = math.ceil(length / 2) + node = self.head + # interate over the found mid_index + for _index in range(mid_index): + node = node.next + return node.data + return None + # Driver code list1 = LinkedList() + list1.push(5) list1.push(4) list1.push(2) list1.push(3) -list1.push(1) -list1.printMiddle() +list1.push(1) +print(list1.printMiddle()) diff --git a/Exercise_4.py b/Exercise_4.py index 9bc25d3d..fc5101a4 100644 --- a/Exercise_4.py +++ b/Exercise_4.py @@ -1,18 +1,66 @@ # Python program for implementation of MergeSort +# Time Complexity : O(n log n) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : No. Could not find this problem (with 1 argument) on geeks or leetcode +# Any problem you faced while coding this : Yes. While implementing the merging logic. def mergeSort(arr): #write your code here - + # dividing the array + if len(arr) > 1: + mid = len(arr) // 2 + lhs = arr[0: mid] + rhs = arr[mid:] + # dividing the lhs of the array until only one element is left in the array + mergeSort(lhs) + # dividing the rhs of the array until only one element is left in the array + mergeSort(rhs) + + # merging the array + # index for lhs and rhs + i, j = 0,0 + # index for the merged array + k = 0 + # merge_list =[] + while i < len(lhs) and j < len(rhs): + # if the left side element is smaller than the right hand side, add into the array + if lhs[i] < rhs[j]: + arr[k] = lhs[i] + # merge_list.append(lhs[i]) + # increment the left element + i += 1 + # else add right side element into the array + else: + arr[k] = rhs[j] + # merge_list.append(rhs[j]) + # increment the right element + j +=1 + # increment the merged array to get the next element + k += 1 + # merging the remaning left side element at the end of the list + while i < len(lhs): + arr[k] = lhs[i] + # merge_list.append(lhs[i]) + i += 1 + k += 1 + # merging the remaning right side element at the end of the list + while j < len(rhs): + arr[k] = rhs[j] + # merge_list.append(rhs[j]) + j +=1 + k +=1 + + # Code to print the list def printList(arr): #write your code here - + print(arr) + # driver code to test the above code -if __name__ == '__main__': - arr = [12, 11, 13, 5, 6, 7] - print ("Given array is", end="\n") - printList(arr) - mergeSort(arr) - print("Sorted array is: ", end="\n") - printList(arr) +arr = [12, 11, 13, 5, 6, 7] +print("Given array is", arr) +printList(arr) +mergeSort(arr) +print("Sorted array is: ",arr) +printList(arr) diff --git a/Exercise_5.py b/Exercise_5.py index 1da24ffb..bb3a7c61 100644 --- a/Exercise_5.py +++ b/Exercise_5.py @@ -1,10 +1,61 @@ # Python program for implementation of Quicksort +# Time Complexity : O(log n) +# Space Complexity : O(log n) +# Did this code successfully run on Leetcode : No. Could not find this problem on geeks or leetcode +# Any problem you faced while coding this : Yes. While implementing the Iterative sort logic. # This function is same in both iterative and recursive def partition(arr, l, h): #write your code here + pivot_index = l + pivot_element = arr[pivot_index] + i = pivot_index + 1 + j = h + + while i <= j: + while i <=j and arr[i] <= pivot_element: + i += 1 + + while i <=j and arr[j] >= pivot_element: + j -= 1 + + if i <=j: + arr[i], arr[j] = arr[j], arr[i] + + arr[pivot_index], arr[j] = arr[j], arr[pivot_index] + + return j def quickSortIterative(arr, l, h): #write your code here + stack = [] + + # append starting range to stack + stack.append((l, h)) + + # continue this operation until the stack is empty means all the elements are considered + while stack: + l, h = stack.pop() + + if l < h: + # call the partition function and find the pivot element position(index) + p = partition(arr, l, h) + + # get the left hand side element from the pivot element position(index) + if p > l: + stack.append((l, p)) + + # get the right hand side element from the pivot element position + if p + 1 < h: + stack.append((p + 1, h)) + return arr + +# Driver code to test above +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("i",arr[i]) \ No newline at end of file