diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..3f3893af --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 00000000..c7f20cc5 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + PreCourse-2 + + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 00000000..cca691f6 --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 00000000..49c8cd4f --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.JRE_CONTAINER \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 00000000..11006e2a --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Global \ No newline at end of file diff --git a/BinarySearch.class b/BinarySearch.class new file mode 100644 index 00000000..e0ae2d2f Binary files /dev/null and b/BinarySearch.class differ diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..8027fb25 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,8 +1,25 @@ class BinarySearch { + // Time Complexity : O(log n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : // 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 if(arr[mid] > x) { + r = mid - 1; + } + } + + return -1; } // Driver method to test above diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..4932b834 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -6,13 +6,41 @@ class QuickSort smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ + // Time Complexity : + // Space Complexity : + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : difficult to understand its partitions, not sure about how to get time and space complexity void swap(int arr[],int i,int j){ - //Your code here + //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 + // choose the pivot + int pivot = arr[high]; + + // index of smaller element and indicates + // the right position of pivot found so far + int i = low - 1; + + // traverse arr[low to high] and move all smaller + // elements to the left side. Elements from low to + // i are smaller after every iteration + for (int j = low; j <= high - 1; j++) { + if (arr[j] < pivot) { + i++; + swap(arr, i, j); + } + } + + // Move pivot after smaller elements and + // return its position + swap(arr, i + 1, high); + return i + 1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, @@ -20,8 +48,18 @@ 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 + // Recursively sort elements before + // partition and after partition + if (low < high) { + + // pi is the partition return index of pivot + int pi = partition(arr, low, high); + + // recursion calls for smaller elements + // and greater or equals elements + sort(arr, low, pi - 1); + sort(arr, pi + 1, high); + } } /* A utility function to print array of size n */ diff --git a/Exercise_3.java b/Exercise_3.java index 1f9b752a..7955e6b8 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,5 +1,9 @@ class LinkedList { + // Time Complexity : O(n) + // Space Complexity : O(1) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : Node head; // head of linked list /* Linked list node */ @@ -20,6 +24,18 @@ void printMiddle() { //Write your code here //Implement using Fast and slow pointers + Node slowPtr = head; + Node fastPtr = head; + + if (head != null) + { + while (fastPtr != null && fastPtr.next != null) + { + fastPtr = fastPtr.next.next; + slowPtr = slowPtr.next; + } + System.out.println("The middle element is [" + slowPtr.data + "]"); + } } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..957e001a 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -3,9 +3,59 @@ class MergeSort // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] + // Time Complexity : O(n log n) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : void merge(int arr[], int l, int m, int r) { //Your code here + // Find sizes of two sub arrays to be merged + int n1 = m - l + 1; + int n2 = r - m; + + // Create temp arrays + int L[] = new int[n1]; + int R[] = new int[n2]; + + // Copy data to temp arrays + for (int i = 0; i < n1; ++i) + L[i] = arr[l + i]; + for (int j = 0; j < n2; ++j) + R[j] = arr[m + 1 + j]; + + // Merge the temp arrays + + // Initial indices of first and second subarrays + int i = 0, j = 0; + + // Initial index of merged subarray array + int k = l; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } + else { + arr[k] = R[j]; + j++; + } + k++; + } + + // Copy remaining elements of L[] if any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy remaining elements of R[] if any + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } } // Main function that sorts arr[l..r] using @@ -14,6 +64,18 @@ void sort(int arr[], int l, int r) { //Write your code here //Call mergeSort from here + if (l < r) { + + // Find the middle point + int m = l + (r - l) / 2; + + // Sort first and second halves + sort(arr, l, m); + sort(arr, m + 1, r); + + // Merge the sorted halves + merge(arr, l, m, r); + } } /* A utility function to print array of size n */ diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..c74af510 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,4 +1,8 @@ class IterativeQuickSort { + // Time Complexity : O(n log n) + // Space Complexity : O(n) + // Did this code successfully run on Leetcode : + // Any problem you faced while coding this : difficult to understand through stacks void swap(int arr[], int i, int j) { //Try swapping without extra variable @@ -9,12 +13,66 @@ 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); // index of smaller element + for (int j = l; j <= h - 1; j++) { + // If current element is smaller than or + // equal to pivot + if (arr[j] <= pivot) { + i++; + + // swap + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + // swap arr[i+1] and arr[h] (or pivot) + int temp = arr[i + 1]; + arr[i + 1] = arr[h]; + arr[h] = temp; + + 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. + // Create an auxiliary stack + int[] stack = new int[h - l + 1]; + + int top = -1; + + // push initial values of l and h to stack + stack[++top] = l; + stack[++top] = h; + + // Keep popping from stack while is not empty + while (top >= 0) { + // Pop h and l + h = stack[top--]; + l = stack[top--]; + + // Set pivot element at its correct position + // in sorted array + int p = partition(arr, l, h); + + // If there are elements on left side of pivot, + // then push left side to stack + if (p - 1 > l) { + stack[++top] = l; + stack[++top] = p - 1; + } + + // If there are elements on right side of pivot, + // then push right side to stack + if (p + 1 < h) { + stack[++top] = p + 1; + stack[++top] = h; + } + } } // A utility function to print contents of arr diff --git a/IterativeQuickSort.class b/IterativeQuickSort.class new file mode 100644 index 00000000..0325888e 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..43a7d4c9 Binary files /dev/null and b/LinkedList$Node.class differ diff --git a/LinkedList.class b/LinkedList.class new file mode 100644 index 00000000..6482eabe Binary files /dev/null and b/LinkedList.class differ diff --git a/MergeSort.class b/MergeSort.class new file mode 100644 index 00000000..05051895 Binary files /dev/null and b/MergeSort.class differ diff --git a/QuickSort.class b/QuickSort.class new file mode 100644 index 00000000..8dc7b902 Binary files /dev/null and b/QuickSort.class differ