From ac6dd54fd5e341ed8ee329bedaba6289225b607f Mon Sep 17 00:00:00 2001 From: mukul Date: Sun, 21 Dec 2025 16:09:57 +0530 Subject: [PATCH] Done with Precourse2 --- Exercise_1.java | 18 +++++++++++++++++ Exercise_2.java | 24 ++++++++++++++++++++-- Exercise_3.java | 15 ++++++++++++++ Exercise_4.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ Exercise_5.java | 44 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 2 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..1b20e787 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -3,6 +3,24 @@ class BinarySearch { int binarySearch(int arr[], int l, int r, int x) { //Write your code here + while (l <= r){ + + int m = (l + r) / 2; + + if (arr[m] == x) { + return m; + + } + else if (arr[m] > x) { + r = m - 1; + + } + else { + l = m + 1; + } + } + + return -1; } // Driver method to test above diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..f93b0380 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -7,12 +7,27 @@ 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 + //Write code here for Partition and Swap + int pivot = arr[high]; + 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(arr, i + 1, high); + return i + 1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, @@ -22,6 +37,11 @@ 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); + sort(arr, low, pi - 1); + sort(arr, pi + 1, high); + } } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..51e75742 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -20,6 +20,21 @@ 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) + { + fast_ptr = fast_ptr.next.next; + slow_ptr = slow_ptr.next; + } + + System.out.println("Middle element: " + slow_ptr.data); } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..ff44e455 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -6,6 +6,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; + + // 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; + 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 @@ -14,6 +56,17 @@ void sort(int arr[], int l, int r) { //Write your code here //Call mergeSort from here + if (l < r) { + 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); + } + } /* A utility function to print array of size n */ diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..309a9831 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,7 +1,14 @@ +import java.util.Stack; + class IterativeQuickSort { void swap(int arr[], int i, int j) { //Try swapping without extra variable + if (i != j) { + arr[i] = arr[i] + arr[j]; + arr[j] = arr[i] - arr[j]; + arr[i] = arr[i] - arr[j]; + } } /* This function is same in both iterative and @@ -9,12 +16,49 @@ void swap(int arr[], int i, int j) int partition(int arr[], int l, int h) { //Compare elements and swap. + int pivot = arr[h]; + int i = l - 1; + + for (int j = l; j < h; j++) { + if (arr[j] <= pivot) { + i++; + swap(arr, i, j); + } + } + + swap(arr, i + 1, h); + return i + 1; } // Sorts arr[l..h] using iterative QuickSort void QuickSort(int arr[], int l, int h) { //Try using Stack Data Structure to remove recursion. + Stack stack = new Stack<>(); + + // Push initial values of l and h to stack + stack.push(l); + stack.push(h); + + // Keep popping from stack while it's not empty + while (!stack.isEmpty()) { + h = stack.pop(); + l = stack.pop(); + + int p = partition(arr, l, h); + + // If elements on left side of pivot, push left side to stack + if (p - 1 > l) { + stack.push(l); + stack.push(p - 1); + } + + // If elements on right side of pivot, push right side to stack + if (p + 1 < h) { + stack.push(p + 1); + stack.push(h); + } + } } // A utility function to print contents of arr