diff --git a/Exercise_1.java b/Exercise_1.java index c3ff1141..96d413b6 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,10 +1,50 @@ +// Time Complexity : O(log n) +// Space Complexity :O(1) as we are not creating an extra array to store +// Did this code successfully run on Leetcode : I didn't find similar question on leetcode +// Any problem you faced while coding this : No + + + + 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 + boolean isAsc = arr[l] < arr[r]; // It is checking if the array is sorted in ascending or Descending manner + if(arr[l] < arr[r]){ + isAsc=true; + } + else{ + isAsc=false; + } + + while(l<=r){ //Base condition if l > r then the element will not be present + int mid=l+(r-l)/2; + if(arr[mid]==x){ //If the mid is the target element then return it and no further execution + return mid; + } + else{ + if(isAsc){ //If array is sorted in ascending order + if(x < arr[mid]){ + r=mid-1; + } + else{ + l=mid+1; + } + } + else{ //If array is sorted in descending order + if(x < arr[mid]){ + l=mid+1; + } + else{ + r=mid-1; + } + } + } + } + return -1; //If the target element is not present } - // Driver method to test above public static void main(String args[]) { diff --git a/Exercise_2.java b/Exercise_2.java index d0b5fa5f..74c2cd4c 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,3 +1,8 @@ +// Time Complexity : O(n log n) +// Space Complexity : Not sure +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : Yes, I was not sure how to implement it and I refereed few videos but still i was unable to do it on my own + class QuickSort { /* This function takes last element as pivot, @@ -6,26 +11,50 @@ class QuickSort smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */ - void swap(int arr[],int i,int j){ + void swap(int arr[],int i,int j){ //Swap logic to swap two elements using a temp variable //Your code here + int temp = arr[i]; + arr[i]= arr[j]; + arr[j]=temp; } - int partition(int arr[], int low, int high) + int partition(int arr[], int low, int high) //this is a partition function which compares every element with the pivot and places small elements than pivot at left and greater elements to the right of pivot and returns pivot { + + int pivot = arr[high]; + int x=low-1; //x is the pointer which we are maintaining and it will return the correct position of the pivot + + for(int i=low;i Array to be sorted, low --> Starting index, high --> Ending index */ - void sort(int arr[], int low, int high) - { + void sort(int arr[], int low, int high) //This is the sort function which is recursively called based on the pivot. + { //for example if there are 10 elements and pivot is the sixth element then sort function will be recursively called for 0 to 5 index and 7-10 element and so on. + + if(low I think it should be o(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : I faced issue while finding out the end condition when the loop should stop + class LinkedList { Node head; // head of linked list @@ -18,8 +23,20 @@ class Node //Complete this function void printMiddle() { + Node slow=head; //Two pointers one slow and one fast + Node fast=head; + + + while(fast!=null && fast.next!=null){ //Condition till when the loop should iterate considering both even and odd number of nodes + fast=fast.next.next; //The fast pointer will move twice than slow pointer + slow=slow.next; + } + System.out.println("Middle node is "+slow.data); + + //Write your code here //Implement using Fast and slow pointers + } public void push(int new_data) diff --git a/Exercise_4.java b/Exercise_4.java index 81afd3c2..04c7a0d0 100644 --- a/Exercise_4.java +++ b/Exercise_4.java @@ -1,3 +1,7 @@ +// Time Complexity : O(n log(n)) -> I think it should be o(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : I referred few videos to so this class MergeSort { // Merges two subarrays of arr[]. @@ -6,12 +10,51 @@ class MergeSort void merge(int arr[], int l, int m, int r) { //Your code here + int[] merged = new int[r-l+1]; + int idx1=l; //pointer to first sub array + int idx2=m+1; //pointer to second sub array + int k=0; + while(idx1<=m && idx2 <=r){ + if(arr[idx1] <= arr[idx2] ){ + merged[k] = arr[idx1]; + k++; + idx1++; + } + else{ + merged[k] = arr[idx2]; + k++; + idx2++; + } + } + while(idx1<=m){ //loop that will copy all the remaining any elements in the same order as they will be already sorted + merged[k] = arr[idx1]; + k++; + idx1++; + } + while(idx2<=r){ //loop that will copy all the remaining any elements in the same order as they will be already sorted + merged[k] = arr[idx2]; + k++; + idx2++; + } + + for(int i=0, j=l;i=r){ + return; + } + int mid = l+(r-l)/2; + + + sort(arr,l,mid); + sort(arr,mid+1,r); + merge(arr,l,mid,r); //Write your code here //Call mergeSort from here } diff --git a/Exercise_5.java b/Exercise_5.java index 30e82675..95418a58 100644 --- a/Exercise_5.java +++ b/Exercise_5.java @@ -1,23 +1,68 @@ +// Time Complexity : Not Sure +// Space Complexity : Not sure +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : Yes, I was not able to implement it on my own and understand it. +import java.util.*; class IterativeQuickSort { void swap(int arr[], int i, int j) { - //Try swapping without extra variable + if (i != j) { //it will not swap if i & j are equal + arr[i] = arr[i] + arr[j]; //swapping without using 3rd variable + 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) + int partition(int arr[], int l, int h) //partitioning and returning the position of the pivot { - //Compare elements and swap. + int pivot = arr[h]; + int x=l-1; //x is the pointer which we are maintaining and it will return the correct position of the pivot + + for(int i=l;i stack = new Stack<>(); + stack.push(l); + stack.push(h); + + while (!stack.isEmpty()) { + int high = stack.pop(); + int low = stack.pop(); + + if (low < high) { + int pi = partition(arr, low, high); + + //left side + if (pi - 1 > low) { + stack.push(low); + stack.push(pi - 1); + } + + //right side + if (pi + 1 < high) { + stack.push(pi + 1); + stack.push(high); + } + } + } + + } - // A utility function to print contents of arr + void printArr(int arr[], int n) { int i;