From 9d9e19e64c282a462a5027626bf2f1ebf94616fc Mon Sep 17 00:00:00 2001 From: michaeldonato Date: Sun, 17 Aug 2025 00:30:20 -0400 Subject: [PATCH 1/2] Completed Precourse-2 --- Exercise_1.java | 23 ++++++++++++++--- Exercise_2.java | 38 +++++++++++++++++++-------- Exercise_3.java | 18 ++++++++++++- Exercise_4.java | 68 ++++++++++++++++++++++++++++++++++++++++++------- Exercise_5.java | 59 +++++++++++++++++++++++++++++++++++------- 5 files changed, 174 insertions(+), 32 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..9c22e546 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,9 +1,26 @@ -class BinarySearch { +// Time Complexity : O(n) +// Space Complexity : +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : +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 += 1; + } else if (arr[mid] > x) { + r -= 1; + } + } + } + return -1; + } // Driver method to test above public static void main(String args[]) diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..f9243c8f 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,9 @@ -class QuickSort +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Unlike Binary Search, there was a lot I had to lookup for this one + +class QuickSort { /* This function takes last element as pivot, places the pivot element at its correct @@ -6,22 +11,35 @@ 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){ - //Your code here + + void swap(int arr[], int i, int j){ + 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 partition(int arr[], int low, int high) { + int pivot = arr[high]; + int i = (low - 1); + for (int j = low; j <= high-1; j++) { + 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, low --> Starting index, high --> Ending index */ - void sort(int arr[], int low, int high) - { - // Recursively sort elements before - // partition and after partition + void sort(int arr[], int low, int high) { + if (high > low) { + int pivot = partition(arr, low, high); + sort(arr, low, pivot-1); + sort(arr, pivot+1, high); + } } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..01a97fdc 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,4 +1,8 @@ -class LinkedList +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Had to lookup fast and slow pointers +class LinkedList { Node head; // head of linked list @@ -16,10 +20,22 @@ class Node /* Function to print middle of linked list */ //Complete this function + + // void printMiddle() { //Write your code here //Implement using Fast and slow pointers + if (head == null) { + System.out.println("Null Linked List"); + } + Node fast = head; + Node slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + System.out.println(slow.data); } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..db4fc607 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,19 +1,69 @@ -class MergeSort +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None +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) { + int size1 = m - l + 1; + int size2 = r - m; + + int left[] = new int[size1]; + int right[] = new int[size2]; + + for (int i = 0; i < size1; ++i) { + left[i] = arr[l + i]; + } + + for (int j = 0; j < size2; ++j) { + right[j] = arr[m + 1 + j]; + } + + // Merge the temp arrays + // Initial indexes of first and second subarrays + int i = 0, j = 0; + + int k = l; + while (i < size1 && j < size2) { + if (left[i] <= right[j]) { + arr[k] = left[i]; + i++; + } + else { + arr[k] = right[j]; + j++; + } + k++; + } + + while (i < size1) { + arr[k] = left[i]; + i++; + k++; + } + + while (j < size2) { + arr[k] = right[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) { + if (l < r) { + + int mid = (l + r) / 2; + + sort(arr, l, mid); + sort(arr, mid + 1, r); + + merge(arr, l, mid, r); + } } /* A utility function to print array of size n */ diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..8df71f89 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,20 +1,61 @@ -class IterativeQuickSort { - void swap(int arr[], int i, int j) - { - //Try swapping without extra variable +import java.util.Stack; + +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Unable to get swap to work without tmp. See commented code for attempt +class IterativeQuickSort { + void swap(int arr[], int i, int j) { + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + +// 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 recursive*/ 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-1; 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. + void QuickSort(int arr[], int l, int h) { + Stack s = new Stack<>(); + s.push(0); + s.push(arr.length - 1); + + while (!s.isEmpty()) { + int end = s.pop(); + int start = s.pop(); + + if (start >= end) { + continue; + } + + int pivot = partition(arr, start, end); + if (pivot + 1 < end) { + s.push(pivot + 1); + s.push(end); + } + + if (start < pivot - 1) { + s.push(start); + s.push(pivot - 1); + } + } } // A utility function to print contents of arr From 7bc5fdae2016f8f77f03f952cd549a1244671ced Mon Sep 17 00:00:00 2001 From: michaeldonato Date: Sun, 17 Aug 2025 11:25:17 -0400 Subject: [PATCH 2/2] Corrected Binary Search logic, added comments --- Exercise_1.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 9c22e546..a81f7fb3 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,21 +1,27 @@ -// Time Complexity : O(n) -// Space Complexity : +// Time Complexity : O(logn) +// Space Complexity :O(1) // Did this code successfully run on Leetcode : // Any problem you faced while coding this : 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) - { - while (l < r) { + { + // While the leftmost point of the array is less than or equal to the rightmost + while (l <= r) { + + // Finds middle point of array int mid = l + (r - l) / 2; - + + //If the midpoint is the target, return it if (arr[mid] == x) { return mid; } else { + // If the target is greater than the midpoint, update the left index to be one ahead of the midpoint if (arr[mid] < x) { - l += 1; + l = mid + 1; + // If the target is less than the midpoint, update the right index to be one less the midpoint } else if (arr[mid] > x) { - r -= 1; + r = mid - 1; } } }