diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..bba96914 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,28 @@ +/* + * + * Time Complexity : O(logn) + * Space Complexity : O(1) + * Did this code successfully run on Leetcode : N/A + * Any problem you faced while coding this : No + */ + class BinarySearch { // Returns index of x if it is present in arr[l.. r], else return -1 int binarySearch(int arr[], int l, int r, int x) { //Write your code here + while(l <= r) { + int mid = (l + r) / 2; + if(arr[mid] == x) { + return mid; + } else if(arr[mid] > x) { + r = mid - 1; + } else { + l = mid + 1; + } + } + + return -1; } // Driver method to test above diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..d2e82859 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,3 +1,12 @@ +/* + * + * Time Complexity : O(n^2) ~ Worst case encountered when Array is sorted in descending order + * Space Complexity : O(1) + * Did this code successfully run on Leetcode : Yes + * Any problem you faced while coding this : Struggled to find the base case, weak in recursion + */ + + class QuickSort { /* This function takes last element as pivot, @@ -7,12 +16,34 @@ class QuickSort pivot and all greater elements to right of pivot */ void swap(int arr[],int i,int j){ - //Your code here + //Your code here + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; } int partition(int arr[], int low, int high) { //Write code here for Partition and Swap + + int pivot = high; + int j = low; + int i = j - 1; + + while(j < pivot) { + if(arr[j] < arr[pivot]) { + i++; + swap(arr, i, j); + j++; + } else { + j++; + } + } + + i++; + swap(arr, i, j); + return i; + } /* The main function that implements QuickSort() arr[] --> Array to be sorted, @@ -22,6 +53,14 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + + if(high <= low) { + return; + } + + int pivot = partition(arr, low, high); + sort(arr, low, pivot - 1); + sort(arr, pivot + 1, high); } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..e220c582 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,11 @@ +/* + * + * Time Complexity : O(n) + * Space Complexity : O(1) + * Did this code successfully run on Leetcode : Yes + * Any problem you faced while coding this : No + */ + class LinkedList { Node head; // head of linked list @@ -20,6 +28,15 @@ void printMiddle() { //Write your code here //Implement using Fast and slow pointers + Node slow = head; + Node fast = head; + + while(fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + System.out.println("Middle : " + slow.data); } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..f68f2a01 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,3 +1,16 @@ +/* + * + * Time Complexity : O(nlogn) ~ logn to divide, n to merge + * Space Complexity : O(n) ~ temporary arrays initialized during merge + * Did this code successfully run on Leetcode : Yes + * Any problem you faced while coding this : + * -- Struggled to figure out merge code + * -- Found it quite tricky to play with the indices, specially m and r + * -- Referred to this video ~ https://www.youtube.com/watch?v=SHqvb69Qy70 + + */ + + class MergeSort { // Merges two subarrays of arr[]. @@ -6,6 +19,48 @@ class MergeSort void merge(int arr[], int l, int m, int r) { //Your code here + int n1 = m - l + 1; + int n2 = r - m; + + int[] lArr = new int[n1]; + int[] rArr = new int[n2]; + + for(int i = 0;i st = new Stack<>(); + st.add(new int[]{l,h}); + + while(!st.isEmpty()) { + int[] temp = st.pop(); + int low = temp[0]; + int high = temp[1]; + if(high > low) { + int pivot = partition(arr, low, high); + st.add(new int[]{low, pivot-1}); + st.add(new int[]{pivot+1, high}); + } + } } // A utility function to print contents of arr