diff --git a/Exercise_1.java b/Exercise_1.java index c3ff11410..7d6f50589 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,21 +1,61 @@ +// Time Complexity : O(log n) → Because with each step, we cut down the search space by half. +// Space Complexity : O(1) → No extra space used apart from a few variables. +// 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 - int binarySearch(int arr[], int l, int r, int x) - { - //Write your code here + + /** + * Binary Search Algorithm + * + * @param arr - The sorted input array + * @param l - Left boundary of the search space + * @param r - Right boundary of the search space + * @param x - Element to search for + * @return - Index of the element if found, otherwise -1 + */ + int binarySearch(int arr[], int l, int r, int x) { + // Continue searching while left index <= right index + while (l <= r) { + // ✅ To prevent integer overflow, use this formula + int mid = l + (r - l) / 2; + + // ✅ Case 1: If middle element is equal to target → return mid + if (arr[mid] == x) { + return mid; + } + // ✅ Case 2: If middle element is smaller than target → search right side + else if (arr[mid] < x) { + l = mid + 1; + } + // ✅ Case 3: If middle element is greater than target → search left side + else { + r = mid - 1; + } + } + + // ✅ If element is not present in the array + return -1; } - - // Driver method to test above - public static void main(String args[]) - { + + // ✅ Driver method to test the code + public static void main(String args[]) { BinarySearch ob = new BinarySearch(); + + // ✅ Sample Input and Explanation: + // Sorted array (binary search only works on sorted arrays) int arr[] = { 2, 3, 4, 10, 40 }; int n = arr.length; - int x = 10; + int x = 10; // Element to search for + + // ✅ Call binary search 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); + + // ✅ Output the result + if (result == -1) { + System.out.println("Element not present in the array."); + } else { + System.out.println("Element found at index " + result); + } } -} +} diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f4..a1ce0cc8f 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,3 +1,11 @@ +// Time Complexity :O(NlogN) → Because we are dividing the array into two halves at each step. +// Space Complexity :O(NlogN) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this :No + + +// Your code here along with comments explaining your approach + class QuickSort { /* This function takes last element as pivot, @@ -8,11 +16,24 @@ class QuickSort 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 + //Write code here for Partition and Swap + int pivot = arr[high]; + int swapIndex = low; + for(int i=low; i Array to be sorted, @@ -22,6 +43,11 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + if(low= 0) { + // Pop h and l + h = stack[top--]; + l = stack[top--]; + + // Partition the array and get the pivot + int p = partition(arr, l, h); + + // If there are elements on the left side of pivot, push them to stack + if (p - 1 > l) { + stack[++top] = l; + stack[++top] = p - 1; + } + + // If there are elements on the right side of pivot, push them to stack + if (p + 1 < h) { + stack[++top] = p + 1; + stack[++top] = h; + } + } } - // A utility function to print contents of arr - void printArr(int arr[], int n) - { - int i; - for (i = 0; i < n; ++i) + // A utility function to print contents of array + void printArr(int arr[], int n) { + for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); + System.out.println(); } // Driver code to test above - public static void main(String args[]) - { + 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 +}