From e15317b9dda7be586af1bdd0f4d37828190435dc Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 20 Sep 2025 03:05:02 +0530 Subject: [PATCH] Done Precourse2 --- Exercise_1.java | 21 +++++++- Exercise_2.java | 32 ++++++++++-- Exercise_3.java | 21 ++++++-- Exercise_4.java | 126 ++++++++++++++++++++++++++++++++++-------------- Exercise_5.java | 115 +++++++++++++++++++++++++++++-------------- 5 files changed, 236 insertions(+), 79 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..d7118dd6 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -2,7 +2,23 @@ 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 + if (r >= l) { + int mid = l + (r - l) / 2; + + // Check if x is present at mid + if (arr[mid] == x) + return mid; + + // If x is smaller, it must be in the left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else, it must be in the right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // Element is not present in the array + return -1; } // Driver method to test above @@ -19,3 +35,6 @@ public static void main(String args[]) System.out.println("Element found at index " + result); } } + +//Time complexity : O(log n) +//Space complexity : O(log n) diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..1e735953 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -7,13 +7,27 @@ class QuickSort 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 - } + int pivot = arr[high]; // pivot + int i = (low - 1); // index of smaller element + + for (int j = low; j < high; j++) { + // If current element is smaller than or equal to pivot + if (arr[j] <= pivot) { + i++; + swap(arr, i, j); + } + } + // Swap the pivot element with the element at i+1 + swap(arr, i + 1, high); + return i + 1; + } /* The main function that implements QuickSort() arr[] --> Array to be sorted, low --> Starting index, @@ -22,6 +36,15 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + + if (low < high) { + // Partition index + int pi = partition(arr, low, high); + + // Recursively sort elements before and after partition + sort(arr, low, pi - 1); + sort(arr, pi + 1, high); + } } /* A utility function to print array of size n */ @@ -46,3 +69,6 @@ public static void main(String args[]) printArray(arr); } } + +//Time complexity : O(n log n) +//Space complexity : O(log n) diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..c9858fd0 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -18,8 +18,20 @@ class Node //Complete this function void printMiddle() { - //Write your code here - //Implement using Fast and slow pointers + if (head == null) { + System.out.println("The list is empty."); + return; + } + + Node slow_ptr = head; + Node fast_ptr = head; + + while (fast_ptr != null && fast_ptr.next != null) { + slow_ptr = slow_ptr.next; // move by 1 + fast_ptr = fast_ptr.next.next; // move by 2 + } + + System.out.println("Middle element: " + slow_ptr.data); } public void push(int new_data) @@ -50,4 +62,7 @@ public static void main(String [] args) llist.printMiddle(); } } -} \ No newline at end of file +} + +//Time complexity : O(n) +//Space complexity : O(1) \ No newline at end of file diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..2afaac01 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,42 +1,96 @@ class MergeSort { - // Merges two subarrays of arr[]. - // First subarray is arr[l..m] - // Second subarray is arr[m+1..r] + // 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() + { + // Find sizes of two subarrays to be merged + int n1 = m - l + 1; + int n2 = r - m; + + // Create temp arrays + int L[] = new int[n1]; + int R[] = new int[n2]; + + // Copy data to temp arrays + 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]; + + // Merge the temp arrays + + int i = 0, j = 0; + + // Initial index of merged subarray + int k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + + // Copy remaining elements of L[] + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy remaining elements of R[] + 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 - } - - /* A utility function to print array of size n */ + { + if (l < r) { + // Find the middle point + int m = l + (r - l) / 2; + + // Sort first and second halves + sort(arr, l, m); + sort(arr, m + 1, r); + + // Merge the sorted halves + merge(arr, l, m, r); + } + } + + // Utility function to print array static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i stack = new Stack<>(); + + // Push initial values of l and h to stack + stack.push(l); + stack.push(h); + + // Keep popping until stack is empty + while (!stack.isEmpty()) { + h = stack.pop(); + l = stack.pop(); + + // Partition the array and get pivot index + int p = partition(arr, l, h); + + // If elements on the left of pivot, push left side to stack + if (p - 1 > l) { + stack.push(l); + stack.push(p - 1); + } + + // If elements on the right of pivot, push right side to stack + if (p + 1 < h) { + stack.push(p + 1); + stack.push(h); + } + } + } + + // Utility function to print array + void printArr(int arr[], int n) { + for (int i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + System.out.println(); + } + + // Driver code + 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); + } +} + + +//Time complexity : O(n log n) +//Space complexity : O(log n) \ No newline at end of file