From c9096e3a1c22b50be4e14f39d0e79aa70c49d002 Mon Sep 17 00:00:00 2001 From: Simranb10 Date: Sun, 14 Jun 2026 18:19:25 -0400 Subject: [PATCH] Precourse-2 Solution --- Exercise_1.java | 15 ++++++- Exercise_2.java | 31 +++++++++++--- Exercise_3.java | 11 ++++- Exercise_4.java | 54 +++++++++++++++++++++--- Exercise_5.java | 107 +++++++++++++++++++++++++++++++++--------------- 5 files changed, 172 insertions(+), 46 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..1b8f6e46 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,21 @@ -class BinarySearch { +//Time Complexity : O(log n) +//Space Complexity : O(1) +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 diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..d3d72be2 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,4 +1,6 @@ -class QuickSort +//Time Complexity : O(n log n) in average case, O(n^2) in worst case +//Space Complexity : O(log n) in worst case +class QuickSort { /* This function takes last element as pivot, places the pivot element at its correct @@ -7,13 +9,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; + for (int j = low; j < high; 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, @@ -21,7 +37,12 @@ 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) { + int part = partition(arr, low, high); + sort(arr, low, part - 1); + sort(arr, part + 1, high); + } } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..5e8fceba 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,4 +1,6 @@ -class LinkedList +//Time Complexity: O(n) +//Space Complexity: O(1) +class LinkedList { Node head; // head of linked list @@ -20,6 +22,13 @@ void printMiddle() { //Write your code here //Implement using Fast and slow pointers + 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..59dbeed2 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,19 +1,61 @@ -class MergeSort +//Time Complexity: O(n log n) +//Space Complexity: O(n) +import java.util.Arrays; + +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 + { + int s1 = m - l + 1; + int s2 = r - m; + + int left[] = new int[s1]; + int right[] = new int[s2]; + for (int i = 0; i < s1; i++) { + left[i] = arr[l + i]; + } + for (int j = 0; j < s2; j++) { + right[j] = arr[m + 1 + j]; + } + + int i = 0, j = 0, k = l; + while (i < s1 && j < s2) { + if (left[i] <= right[j]) { + arr[k] = left[i]; + i++; + } else { + arr[k] = right[j]; + j++; + } + k++; + } + + while (i < s1) { + arr[k] = left[i]; + i++; + k++; + } + + while (j < s2) { + 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 + { + if (l < r) { + int mid = l + (r - l) / 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..2a14ba01 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,36 +1,77 @@ -class IterativeQuickSort { - void swap(int arr[], int i, int j) - { - //Try swapping without extra variable - } - - /* This function is same in both iterative and +//Time Complexity : O(n log n) in average case, O(n^2) in worst case +//Space Complexity : O(log n) in average case, O(n) in worst case + +class IterativeQuickSort { + void swap(int arr[], int i, int j) + { + //Try swapping without extra variable + if(i==j) { + return; + } + 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) - { + int partition(int arr[], int l, int h) + { //Compare elements and swap. - } - - // Sorts arr[l..h] using iterative QuickSort - void QuickSort(int arr[], int l, int h) - { + 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. - } - - // A utility function to print contents of arr - void printArr(int arr[], int n) - { - int i; - for (i = 0; i < n; ++i) - System.out.print(arr[i] + " "); - } - - // 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.QuickSort(arr, 0, arr.length - 1); - ob.printArr(arr, arr.length); - } -} \ No newline at end of file + int[] stack = new int[h - l + 1]; + int top = -1; + stack[++top] = l; + stack[++top] = h; + + while (top >= 0) { + h = stack[top--]; + l = stack[top--]; + + int part = partition(arr, l, h); + + if (part - 1 > l) { + stack[++top] = l; + stack[++top] = part - 1; + } + + if (part + 1 < h) { + stack[++top] = part + 1; + stack[++top] = h; + } + } + + } + + // A utility function to print contents of arr + void printArr(int arr[], int n) + { + int i; + for (i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + } + + // 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.QuickSort(arr, 0, arr.length - 1); + ob.printArr(arr, arr.length); + } +} \ No newline at end of file