From c2ad1950c59a9b97fcec9cfb7a6d6b2127a2bbdc Mon Sep 17 00:00:00 2001 From: Manassa2000 Date: Mon, 6 Oct 2025 12:32:20 -0400 Subject: [PATCH] PreCourse-2 --- Exercise_1.java | 15 +++++++++++++++ Exercise_2.java | 26 +++++++++++++++++++++++++- Exercise_3.java | 13 +++++++++++++ Exercise_4.java | 46 +++++++++++++++++++++++++++++++++++++++++++++- Exercise_5.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 2 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..ad251bd1 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,23 @@ +//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 m = l+(r-l)/2; + if(arr[m]==x){ + return m; + } + else if(arr[m] Array to be sorted, @@ -22,6 +40,12 @@ void sort(int arr[], int low, int high) { // Recursively sort elements before // partition and after partition + if (low < high) { + + 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..4ea8152f 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,5 @@ +//Time Complexity:O(n) +//Space Complexity:O(1) class LinkedList { Node head; // head of linked list @@ -20,6 +22,17 @@ void printMiddle() { //Write your code here //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;//when slow moves 1 step + fast=fast.next.next;//fast moves 2 steps + } + 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..dd5756c8 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,3 +1,5 @@ +//Time Complexity:O(n log n) +//Space Complexity:O(n) class MergeSort { // Merges two subarrays of arr[]. @@ -5,7 +7,43 @@ class MergeSort // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { - //Your code here + //Your code here + //sizes of two subarrays + int size1 = m-l+1; + int size2=r-m; + //create subarrays + int left[]=new int[size1]; + int right[]=new int[size2]; + //put data into left and right + 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 left and right + int i=0,j=0,k=l; + while (i < size1 && j < size2) { + if (left[i] <= right[j]) { + arr[k] = left[i]; + i++; + } + else { + arr[k] = right[j]; + j++; + } + k++; + } + // Copy remaining elements + 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 @@ -14,6 +52,12 @@ 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;// Find middle + sort(arr, l, mid);// Sort first and second halves + sort(arr, mid + 1, r); + merge(arr, l, mid, r);// Merge sorted halves + } } /* A utility function to print array of size n */ diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..e7b180f3 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,7 +1,16 @@ +//Time Complexity:O(n log n) +//Space Complexity:O(n) +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 +18,47 @@ void swap(int arr[], int i, int j) int partition(int arr[], int l, int h) { //Compare elements and swap. + int pivot = arr[h];//fixing pivot + //move from low to high and put elements smaller than pivot on left side + int i=l-1; + for (int j = l; j <= h - 1; j++) { + if (arr[j] < pivot) { + i++; + swap(arr, i, j); + } + } + //move pivot + 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<>(); + if(l l) { + stack.push(l); + stack.push(pivot - 1); + } + + if (pivot + 1 < h) { + stack.push(pivot + 1); + stack.push(h); + } + } + } // A utility function to print contents of arr