From ff1899245bcce7c48b455c53436a1b69a072d0bc Mon Sep 17 00:00:00 2001 From: JoseRMarcano Date: Sat, 27 Dec 2025 10:49:14 -0800 Subject: [PATCH] pre course classes --- Exercise_1.java | 22 ++++++++++++-- Exercise_2.java | 53 ++++++++++++++++++++++++-------- Exercise_3.java | 19 ++++++++++-- Exercise_4.java | 81 +++++++++++++++++++++++++++++++++++++++++++------ Exercise_5.java | 55 +++++++++++++++++++++++++++++++-- 5 files changed, 202 insertions(+), 28 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..e542bbb9 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -2,9 +2,27 @@ 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) { + int high= arr.length; + int low=0; + int mid=0; + //Write your code here - } - + + while (low<=high) { + mid=low+(high-low)/2; // high-low to avoid integer overflow + if (arr[mid]==x) { + return mid; + }else if (arr[mid]>x) { + high=mid-1; + }else { + low = mid+1; + } + } + return -1; + } +} + +public class Exercise_1{ // Driver method to test above public static void main(String args[]) { diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..d98ba266 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -6,13 +6,27 @@ class QuickSort smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ - void swap(int arr[],int i,int j){ + void swap(int arr[],int low,int high){ //Your code here + if (low == high) return; + int tmp = arr[low]; + arr[low] = arr[high]; + arr[high] = tmp; } int partition(int arr[], int low, int high) { //Write code here for Partition and Swap + int pivot = arr[high]; + int i = low; // place for next <= pivot + for (int j = low; j < high; j++) { + if (arr[j] <= pivot) { + swap(arr, i, j); + i++; + } + } + swap(arr, i, high); + return i; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, @@ -22,27 +36,42 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + if (low >= high) return; + int p = partition(arr, low, high); + sort(arr, low, p - 1); + sort(arr, p + 1, high); + } - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i0; --i) @@ -50,4 +65,4 @@ public static void main(String [] args) llist.printMiddle(); } } -} \ No newline at end of file +} \ No newline at end of file diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..4ca34840 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -6,6 +6,50 @@ class MergeSort 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 + 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[], 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 @@ -14,18 +58,26 @@ void sort(int arr[], int l, int r) { //Write your code here //Call mergeSort from here + if (l >= r) + return; + + 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 */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i stack = new ArrayDeque<>(); + stack.push(new int[]{0, arr.length - 1}); + + while (!stack.isEmpty()) { + int[] range = stack.pop(); + int lo = range[0], hi = range[1]; + if (lo >= hi) continue; + + int p = partition(arr, lo, hi); + + // Optimization: push larger partition first to keep stack smaller + int leftSize = (p - 1) - lo; + int rightSize = hi - (p + 1); + + if (leftSize > rightSize) { + stack.push(new int[]{lo, p - 1}); + stack.push(new int[]{p + 1, hi}); + } else { + stack.push(new int[]{p + 1, hi}); + stack.push(new int[]{lo, p - 1}); + } + } + } // A utility function to print contents of arr @@ -25,12 +68,20 @@ void printArr(int arr[], int n) System.out.print(arr[i] + " "); } - // Driver code to test above + +} + + +public class Exercise_5{ + + // 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.printArr(arr, arr.length); + System.out.println("Printed before sorting"); ob.QuickSort(arr, 0, arr.length - 1); ob.printArr(arr, arr.length); } -} \ No newline at end of file +} \ No newline at end of file