diff --git a/Exercise_1.java b/BInarySearch.java similarity index 61% rename from Exercise_1.java rename to BInarySearch.java index c3ff1141..e3ecdb26 100644 --- a/Exercise_1.java +++ b/BInarySearch.java @@ -1,21 +1,35 @@ -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 - } - - // Driver method to test above - public static void main(String args[]) - { - BinarySearch ob = new BinarySearch(); - int arr[] = { 2, 3, 4, 10, 40 }; - int n = arr.length; - int x = 10; - int result = ob.binarySearch(arr, 0, n - 1, x); - if (result == -1) - System.out.println("Element not present"); - else - System.out.println("Element found at index " + result); - } -} +//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 + if (r >= l) { + int mid = l + (r - l) / 2; + if (arr[mid] == x) { + return mid; + } + if (arr[mid] > x) { + return binarySearch(arr, l, mid - 1, x); + } + return binarySearch(arr, mid + 1, r, x); + } + return -1; + } + + // Driver method to test above + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } +} diff --git a/Exercise_5.java b/Exercise_5.java deleted file mode 100644 index 30e82675..00000000 --- a/Exercise_5.java +++ /dev/null @@ -1,36 +0,0 @@ -class IterativeQuickSort { - void swap(int arr[], int i, int j) - { - //Try swapping without extra variable - } - - /* This function is same in both iterative and - recursive*/ - 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) - { - //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 diff --git a/IterativeQuickSort.java b/IterativeQuickSort.java new file mode 100644 index 00000000..e5a46d15 --- /dev/null +++ b/IterativeQuickSort.java @@ -0,0 +1,73 @@ +//Time Complexity : O(n log n) +//Space Complexity : O(log n) in worst case and O(1) in best case +class IterativeQuickSort +{ + void swap(int arr[], int i, int j) + { + //Try swapping without extra variable + 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; 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. + 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 pi = partition(arr, l, h); + if (pi - 1 > l) + { + stack[++top] = l; + stack[++top] = pi - 1; + } + if (pi + 1 < h) + { + stack[++top] = pi + 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 diff --git a/Exercise_4.java b/MergeSort.java similarity index 54% rename from Exercise_4.java rename to MergeSort.java index 81afd3c2..1eccdc57 100644 --- a/Exercise_4.java +++ b/MergeSort.java @@ -1,42 +1,78 @@ -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 - } - - // 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 - } - - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i 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 - } - - /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + // Recursively sort elements before + int pi = partition(arr, low, high); + // partition and after partition + if (low < pi - 1) { + sort(arr, low, pi - 1); + } + if (pi + 1 < high) { + sort(arr, pi + 1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i"); - tnode = tnode.next; - } - System.out.println("NULL"); - } - - public static void main(String [] args) - { - LinkedList llist = new LinkedList(); - for (int i=15; i>0; --i) - { - llist.push(i); - llist.printList(); - llist.printMiddle(); - } - } +class LinkedList +{ + Node head; // head of linked list + + /* Linked list node */ + class Node + { + int data; + Node next; + Node(int d) + { + data = d; + next = null; + } + } + + /* Function to print middle of linked list */ + //Complete this function + void printMiddle() + { + //Write your code here + //Implement using Fast and slow pointers + Node slow = head; + Node fast = head; + if (head != null) { + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + System.out.println("Middle element: " + slow.data); + } + } + + public void push(int new_data) + { + Node new_node = new Node(new_data); + new_node.next = head; + head = new_node; + } + + public void printList() + { + Node tnode = head; + while (tnode != null) + { + System.out.print(tnode.data+"->"); + tnode = tnode.next; + } + System.out.println("NULL"); + } + + public static void main(String [] args) + { + LinkedList llist = new LinkedList(); + for (int i=15; i>0; --i) + { + llist.push(i); + llist.printList(); + llist.printMiddle(); + } + } } \ No newline at end of file