diff --git a/BinarySearch.class b/BinarySearch.class new file mode 100644 index 00000000..1f91b32a Binary files /dev/null and b/BinarySearch.class differ diff --git a/Exercise_1.class b/Exercise_1.class new file mode 100644 index 00000000..cd869c32 Binary files /dev/null and b/Exercise_1.class differ diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..0ebbda68 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,21 +1,42 @@ -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"); +// Time Complexity : O(logn) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : No, Have implemented this before so it was easy + +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) { + // Using the ittrative approch instead of recursive + int result = -1; + if (arr.length == 0) { + return -1; + } + while (l < r) { + int mid = (l + r) / 2; + if (arr[mid] == x) { + result = mid; + break; + } + if (arr[mid] > x) { + r = mid - 1; + } + if (arr[mid] < x) { + l = mid + 1; + } + } + return result; + } + + // 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); - } -} + System.out.println("Element found at index " + result); + } +} diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..84a05db3 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,48 +1,75 @@ -class QuickSort -{ - /* This function takes last element as pivot, - places the pivot element at its correct - position in sorted array, and places all - smaller (smaller than pivot) to left of - pivot and all greater elements to right - of pivot */ - void swap(int arr[],int i,int j){ - //Your code here + +// Time Complexity :O( n log n) +// Space Complexity :O(log n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : I had trouble warpping my head around partition and recursion but after watch a few videos and reading a few article I was able to write it but i am still not 100% confident on this one + +class QuickSort { + /* + * This function takes last element as pivot, + * places the pivot element at its correct + * position in sorted array, and places all + * smaller (smaller than pivot) to left of + * pivot and all greater elements to right + * of pivot + */ + void swap(int arr[], int i, int j) { + int temp; + 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 - } - /* 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 - } - + + int partition(int arr[], int low, int high) { + int pivot = arr[low]; + int i = low - 1; + int j = high + 1; + while (i < j) { + do { + i++; + } while (arr[i] < pivot); + do { + j--; + } while (arr[j] > pivot); + if (i < j) { + swap(arr, i, j); + } + } + swap(arr, low, j); + return j; + } + + /* + * 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) { + if (low < high) { + int pivot = partition(arr, low, high); + sort(arr, low, pivot); + sort(arr, (pivot + 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(); - } - } -} \ No newline at end of file + // Complete this function + void printMiddle() { + Node fast = head; + Node slow = head; + while (fast != null && fast.next != null) { + + slow = slow.next; + fast = fast.next.next; + } + System.out.println("------------------------------------"); + System.out.println(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 diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..50ceb509 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,42 +1,75 @@ -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 - } - +// Time Complexity : O(n log n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this :I was easy + +import java.util.ArrayList; + +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) { + // Actual logic where elements are swapped according to pivot happens in this + // function + ArrayList temp = new ArrayList<>(); + int left = l; + int right = m + 1; + while (left <= m && right <= r) { + if (arr[left] <= arr[right]) { + temp.add(arr[left]); + left++; + } else { + temp.add(arr[right]); + right++; + } + } + while (left <= m) { + temp.add(arr[left]); + left++; + } + while (right <= r) { + temp.add(arr[right]); + right++; + } + for (int i = l; i <= r; i++) { + arr[i] = temp.get(i - l); + } + + } + + // Main function that sorts arr[l..r] using + // merge() + void sort(int arr[], int l, int r) { + if (l >= r) { + return; + } + int m = (l + r) / 2; + sort(arr, l, m); + sort(arr, (m + 1), r); + merge(arr, l, m, r); + + } + /* A utility function to print array of size n */ - static void printArray(int arr[]) - { - int n = arr.length; - for (int i=0; i pivot); + if (left < high) { + swap(arr, left, high); + } + } + swap(arr, l, high); + return high; + } + + // Sorts arr[l..h] using iterative QuickSort + void QuickSort(int arr[], int l, int h) { + 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 pivot = partition(arr, l, h); + if (pivot - 1 > l) { + stack[++top] = l; + stack[++top] = pivot - 1; + } + if (pivot + 1 < h) { + stack[++top] = pivot + 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/IterativeQuickSort.class b/IterativeQuickSort.class new file mode 100644 index 00000000..aae969dc Binary files /dev/null and b/IterativeQuickSort.class differ diff --git a/LinkedList$Node.class b/LinkedList$Node.class new file mode 100644 index 00000000..813d152a Binary files /dev/null and b/LinkedList$Node.class differ diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 00000000..f9bbfd68 Binary files /dev/null and b/LinkedList.class differ diff --git a/MergeSort.class b/MergeSort.class new file mode 100644 index 00000000..67db38a1 Binary files /dev/null and b/MergeSort.class differ diff --git a/QuickSort.class b/QuickSort.class new file mode 100644 index 00000000..07a8a52f Binary files /dev/null and b/QuickSort.class differ diff --git a/tempCodeRunnerFile.java b/tempCodeRunnerFile.java new file mode 100644 index 00000000..0fff88e2 --- /dev/null +++ b/tempCodeRunnerFile.java @@ -0,0 +1 @@ +LinkedList \ No newline at end of file