From 1d9f06ed42817c0bed133084be09af6745221754 Mon Sep 17 00:00:00 2001 From: Shrriang Joshi Date: Sun, 19 Jan 2025 13:05:03 +0530 Subject: [PATCH 1/2] Done Precourse-2 --- Exercise_1.java | 23 +++++++-- Exercise_2.java | 40 ++++++++++++++-- Exercise_3.java | 24 ++++++++-- Exercise_4.java | 122 ++++++++++++++++++++++++++++++++++-------------- Exercise_5.java | 56 +++++++++++++++++++--- 5 files changed, 212 insertions(+), 53 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..40aadb5d 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,18 +1,33 @@ -class BinarySearch { +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 - l)/2; + + if (arr[mid] == x) { + return mid; + } else if (arr[mid] < x) { + l = mid + 1; + } else { + r = mid - 1; + } + } + + return -1; } // Driver method to test above public static void main(String args[]) { - BinarySearch ob = new BinarySearch(); + 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); + int x = 2; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) System.out.println("Element not present"); else diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..1d7a84b6 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -7,13 +7,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 - } + //Write code here for Partition and Swap + + int pivot = arr[high]; + int i = low; + + for (int j = low; j < high; j++) { + + if (arr[j] < pivot) { + swap(arr, i, j); + i++; + } + } + + // Swap the pivot element with the element at index i + swap(arr, i, high); + + // Return the pivot index + return i; + } + /* The main function that implements QuickSort() arr[] --> Array to be sorted, low --> Starting index, @@ -21,7 +42,16 @@ int partition(int arr[], int low, int high) void sort(int arr[], int low, int high) { // Recursively sort elements before - // partition and after partition + // partition and after partition + + if (low < high) { + // Partition the array + int pivot = partition(arr, low, high); + +// Recursively sort elements before and after partition + sort(arr, low, pivot - 1); + sort(arr, pivot + 1, high); + } } /* A utility function to print array of size n */ @@ -36,7 +66,7 @@ static void printArray(int arr[]) // Driver program public static void main(String args[]) { - int arr[] = {10, 7, 8, 9, 1, 5}; + int arr[] = {10, 7, 8, 9, 1, 5, 2}; int n = arr.length; QuickSort ob = new QuickSort(); diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..2f30cf62 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -9,8 +9,8 @@ class Node Node next; Node(int d) { - data = d; - next = null; + this.data = d; + this.next = null; } } @@ -19,8 +19,24 @@ class Node void printMiddle() { //Write your code here - //Implement using Fast and slow pointers - } + //Implement using Fast and slow pointers + + if(head == null) { + System.out.println("list is empty"); + + return; + } + + Node slow = head; + Node fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + System.out.println("Middle element is --> " + slow.data); + } public void push(int new_data) { diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..d2edb305 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,42 +1,96 @@ -class MergeSort -{ +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 - } - + void merge(int arr[], int l, int m, int r) { + //Your code here + + // Sizes of the two subarrays + int n1 = m - l + 1; + int n2 = r - m; + + // Temporary arrays to hold the data + int L[] = new int[n1]; + int R[] = new int[n2]; + + // Copy data to temporary 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]; + } + + int i = 0, j = 0, 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[], if any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy remaining elements of R[], if any + 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 - } - + void sort(int arr[], int l, int r) { + //Write your code here + //Call mergeSort from here + + if (l < r) { + // Find the middle point + int m = l + (r - l) / 2; + + // Recursively 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 */ - 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 the stack + stack.push(new int[] { l, h }); + + while (!stack.isEmpty()) { + // Pop h and l + int[] range = stack.pop(); + l = range[0]; + h = range[1]; + + // Set pivot element at its correct position + int p = partition(arr, l, h); + + // If there are elements on the left side of pivot, push left side to stack + if (p - 1 > l) { + stack.push(new int[] { l, p - 1 }); + } + + // If there are elements on the right side of pivot, push right side to stack + if (p + 1 < h) { + stack.push(new int[] { p + 1, h }); + } + } } // A utility function to print contents of arr @@ -29,8 +72,9 @@ void printArr(int arr[], int n) 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); +// int arr[] = { 4, 3, 5, 2, 1, 3, 2, 3 }; + int arr[] = { 5, 4, 3, 2, 1 }; + ob.QuickSort(arr, 0, arr.length - 1); ob.printArr(arr, arr.length); } } \ No newline at end of file From e05812858ece7b4a5a26941001d3630841af731f Mon Sep 17 00:00:00 2001 From: Shrirang Joshi <37568667+shrirangjoshi94@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:47:51 +0530 Subject: [PATCH 2/2] Update Exercise_1.java --- Exercise_1.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Exercise_1.java b/Exercise_1.java index 40aadb5d..3c43344f 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,3 +1,4 @@ +// Time complexity O(1) best case average or worst case O(logN). 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)