diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..c53ed777 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,9 +1,31 @@ class BinarySearch { - // Returns index of x if it is present in arr[l.. r], else return -1 + // Returns index of x if it is present in arr[l.. r], else return -1 + //time - O(log n) + // space - O(1) int binarySearch(int arr[], int l, int r, int x) { //Write your code here - } + if (r == 0) // if size of array is zero then return -1 + return -1; + 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[]) diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..0de69bd1 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -6,22 +6,41 @@ class QuickSort smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ + // time - O(nlog(n)) + // space - O(n) void swap(int arr[],int i,int j){ + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; //Your code here } 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, 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(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..2453a866 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,5 +1,6 @@ class LinkedList -{ +{ + //time - O(n) Node head; // head of linked list /* Linked list node */ @@ -20,6 +21,14 @@ void printMiddle() { //Write your code here //Implement using Fast and slow pointers + Node fast = head; + Node slow = head; + while(fast != null && fast.next != null) + { + slow = slow.next; + fast = fast.next.next; + } + System.out.println(slow.data); } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..cd4cc47c 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -2,18 +2,57 @@ class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] - // Second subarray is arr[m+1..r] + // Second subarray is arr[m+1..r] + // Time - O(n log n) + // space - O(n) void merge(int arr[], int l, int m, int r) { - //Your code here - } + //Your code here + int n1 = m - l + 1; // size of first array + int n2 = r - m; + int [] subArray1 = new int[n1]; + int [] subarray2 = new int[n2]; + for(int i=0;i stack = new Stack<>(); + stack.push(new int[]{l,h}); + + while (!stack.isEmpty()){ + int[] temp = stack.pop(); + l = temp[0]; + h = temp[1]; + if(l < h){ + int pi = partition(arr, l, h); + stack.push(new int[]{l,pi-1}); + stack.push(new int[]{pi+1, h}); + } + } + } // A utility function to print contents of arr