diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..bdeff9b1 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,24 @@ +// 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) / 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..2de9ef22 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,3 +1,6 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) for recursive stack + class QuickSort { /* This function takes last element as pivot, @@ -7,12 +10,24 @@ class QuickSort pivot and all greater elements to right of pivot */ void swap(int arr[],int i,int j){ - //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 + int partition(int arr[], int low, int high) { + + 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, @@ -20,8 +35,11 @@ int partition(int arr[], int low, int high) high --> Ending index */ 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..304e949d 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,3 +1,6 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + class LinkedList { Node head; // head of linked list @@ -18,8 +21,18 @@ class Node //Complete this function void printMiddle() { - //Write your code here - //Implement using Fast and slow pointers + if (head == null) { + System.out.println("The list is empty"); + return; + } + + Node slow = head; + Node fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + System.out.println("The middle element is " + slow.data); } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..bf6549c8 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,19 +1,60 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + 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 len1 = m - l + 1; + int len2 = r - m; + int[] left = new int[len1]; + int[] right = new int[len2]; + + for (int i = 0; i < len1; i++) { + left[i] = arr[l+i]; + } + for (int j = 0; j < len2; j++) { + right[j] = arr[m+1+j]; + } + int i = 0; // index of left array + int j = 0; // index of right array + int k = l; // index of merged array + + while(i < len1 && j < len2) { + if (left[i] <= right[j]) { + arr[k] = left[i]; + i++; + } else { + arr[k] = right[j]; + j++; + } + k++; + } + while (i < len1) { + arr[k] = left[i]; + i++; + k++; + } + while (j < len2) { + 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) / 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..5cf69396 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,7 +1,13 @@ +import java.util.*; +// Time Complexity: O(n log n) +// Space Complexity: O(n) + class IterativeQuickSort { void swap(int arr[], int i, int j) { - //Try swapping without extra variable + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; } /* This function is same in both iterative and @@ -9,12 +15,40 @@ void swap(int arr[], int i, int j) 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; 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. + Stack stack = new Stack<>(); + stack.push(l); + stack.push(h); + + while (!stack.isEmpty()) { + h = stack.pop(); + l = stack.pop(); + int pivot = partition(arr, l, h); + + if (pivot-1 > 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