diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..690f2751 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,21 +1,42 @@ -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 - } - - // Driver method to test above - public static void main(String args[]) - { - BinarySearch ob = new BinarySearch(); - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = ob.binarySearch(arr, 0, n - 1, x); - if (result == -1) - System.out.println("Element not present"); +// Time Complexity : O(log n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// 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 + // In binary search we divide the array into half repeatedly to search for the + // target value + // Works for sorted list only + int binarySearch(int arr[], int l, int r, int x) { + // Write your code here + while (l <= r) { + int midPosition = (l + r) / 2; + int mid = arr[midPosition]; + if (x == mid) { + // If target value is equal to the mid value return index of mid value + return midPosition; + } else if (x < mid) { + // If target value is less than the mid value, search in the left sub array + r = midPosition - 1; + } else if (x > mid) { + // If target value is greater than the mid value, search in the right sub array + l = midPosition + 1; + } + } + return -1; + } + + // Driver method to test above + public static void main(String args[]) { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); else - System.out.println("Element found at index " + result); - } -} + System.out.println("Element found at index " + result); + } +} diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..db77412e 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,48 +1,83 @@ -class QuickSort -{ - /* This function takes last element as pivot, - places the pivot element at its correct - position in sorted array, and places all - smaller (smaller than pivot) to left of - pivot and all greater elements to right - of pivot */ - void swap(int arr[],int i,int j){ - //Your code here +// Time Complexity : Not sure +// Space Complexity : Not sure +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : Referred some documention and videos and coded. Not getting what will be time complexity + +class QuickSort { + /* + * This function takes last element as pivot, + * places the pivot element at its correct + * position in sorted array, and places all + * smaller (smaller than pivot) to left of + * pivot and all greater elements to right + * of pivot + */ + void swap(int arr[], int i, int j) { + // 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 - } - /* The main function that implements QuickSort() - arr[] --> Array to be sorted, - low --> Starting index, - high --> Ending index */ - void sort(int arr[], int low, int high) - { - // Recursively sort elements before - // partition and after partition - } - + + int partition(int arr[], int low, int high) { + + // Write code here for Partition and Swap + // Taking the last element as pivot + int pivot = arr[high]; + // Pointer for swap position + int i = low - 1; + // Another pointer j that will start from first element, comparing each element + // with pivot + for (int j = low; j < high; j++) { + // If any element less than pivot, swap it the element at index i + if (arr[j] <= pivot) { + i++; + swap(arr, i, j); + } + } + // Swap pivot and place it in correct position + swap(arr, i + 1, high); + return i + 1; + } + + /* + * The main function that implements QuickSort() + * arr[] --> Array to be sorted, + * low --> Starting index, + * high --> Ending index + */ + void sort(int arr[], int low, int high) { + // Recursively sort elements before + // partition and after partition + + if (low < high) { + int pi = partition(arr, low, high); + // System.out.println(pi); + // Recursively call sort + sort(arr, low, pi - 1); + sort(arr, pi + 1, high); + + } + + } + /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i"); - tnode = tnode.next; - } - System.out.println("NULL"); - } - - public static void main(String [] args) - { - LinkedList llist = new LinkedList(); - for (int i=15; i>0; --i) - { - llist.push(i); - llist.printList(); - llist.printMiddle(); - } - } -} \ No newline at end of file + // Complete this function + void printMiddle() { + // Start both slow and fast pointer from head + Node slp = head; + Node fp = head; + + while (fp != null && fp.next != null) { + // Increment fast pointer by 2 till last element + fp = fp.next.next; + // Increment slow pointer by 1 + slp = slp.next; + + } + // As soon as the fast pointer reaches the end, the slow pointer will reach at + // the mid position + System.out.println("Middle " + slp.data); + } + + public void push(int new_data) { + Node new_node = new Node(new_data); + new_node.next = head; + head = new_node; + } + + public void printList() { + Node tnode = head; + while (tnode != null) { + System.out.print(tnode.data + "->"); + tnode = tnode.next; + } + System.out.println("NULL"); + } + + public static void main(String[] args) { + LinkedList llist = new LinkedList(); + for (int i = 15; i > 0; --i) { + llist.push(i); + llist.printList(); + llist.printMiddle(); + } + } +} \ No newline at end of file diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..9ba87eb4 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,42 +1,97 @@ -class MergeSort -{ - // Merges two subarrays of arr[]. - // First subarray is arr[l..m] - // Second subarray is arr[m+1..r] - void merge(int arr[], int l, int m, int r) - { - //Your code here - } - - // Main function that sorts arr[l..r] using - // merge() - void sort(int arr[], int l, int r) - { - //Write your code here - //Call mergeSort from here - } - +// Time Complexity : Not sure +// Space Complexity : Not sure +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : Referred some documention and videos and coded but did not understood properly + +class MergeSort { + // Merges two subarrays of arr[]. + // First subarray is arr[l..m] + // Second subarray is arr[m+1..r] + void merge(int arr[], int l, int m, int r) { + // Your code here + // length of two arrays to merge + int n1 = m - l + 1; + int n2 = r - m; + + // Declare 2 arrays + int[] L = new int[n1]; + int[] R = new int[n2]; + + // For each of the two arrays, add values + for (int i = 0; i < n1; i++) { + L[i] = arr[l + i]; + + } + for (int j = 0; j < n2; j++) { + R[j] = arr[m + 1 + j]; + + } + int i = 0, j = 0; + int k = l; + + // Then compare two arrays and insert in original one to get the final sorted + // array + while (i < n1 && j < n2) { + if (L[i] < R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + + } + + // For remaining elements in any of the one array, simply add all + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } + + } + + // Main function that sorts arr[l..r] using + // merge() + void sort(int arr[], int l, int r) { + // Write your code here + // Call mergeSort from here + + if (l < r) { + int m = (l + (r)) / 2; + // Divide till it is single element + sort(arr, l, m); + sort(arr, m + 1, r); + // Merge the sorted arrays + merge(arr, l, m, r); + } + } + /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i stack = new Stack<>(); + // Push the low and high values + stack.push(l); + stack.push(h); + + while (!stack.isEmpty()) { + // Pop from the stack and get the partion index and iteratively repeat it on the + // sub arrays + int high = stack.pop(); + int low = stack.pop(); + if (low < high) { + int pi = partition(arr, low, high); + + stack.push(low); + stack.push(pi - 1); + + stack.push(pi + 1); + stack.push(high); + } + + } + + } + + // A utility function to print contents of arr + void printArr(int arr[], int n) { + int i; + for (i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + } + + // Driver code to test above + public static void main(String args[]) { + IterativeQuickSort ob = new IterativeQuickSort(); + int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; + ob.QuickSort(arr, 0, arr.length - 1); + ob.printArr(arr, arr.length); + } +} \ No newline at end of file