diff --git a/Exercise_1.cpp b/Exercise_1.cpp index a6dc14cc..675148c0 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -1,3 +1,5 @@ +//Time Complexity : O(logn) +// Space Complexity : O(logn) #include // A recursive binary search function. It returns @@ -5,7 +7,20 @@ // otherwise -1 int binarySearch(int arr[], int l, int r, int x) { - //Your Code here + if(l > r){ + return -1; + } + int mid = l + (r-1) / 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); } int main(void) diff --git a/Exercise_2.cpp b/Exercise_2.cpp index c90e577e..1f7e2332 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -4,7 +4,9 @@ using namespace std; // A utility function to swap two elements void swap(int* a, int* b) { - //Your Code here + int temp = *a; + *a = *b; + *b = temp; } /* This function takes last element as pivot, places @@ -14,7 +16,18 @@ to left of pivot and all greater elements to right of pivot */ int partition (int arr[], int low, int high) { - //Your Code here + int pivot = arr[high]; + int i = low - 1; + + for(int j = low;j <= high -1 ; j++){ + if (arr[j] < pivot){ + + i++; + swap(&arr[i], &arr[j]); + } + swap(&arr[i + 1], &arr[high]); + return i + 1; + } } /* The main function that implements QuickSort @@ -23,7 +36,12 @@ low --> Starting index, high --> Ending index */ void quickSort(int arr[], int low, int high) { - //Your Code here + if (low < high) + { + int pi = partition(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } } /* Function to print an array */ diff --git a/Exercise_3.cpp b/Exercise_3.cpp index 209ce0fe..cb19e7cb 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -11,8 +11,19 @@ struct Node /* Function to get the middle of the linked list*/ void printMiddle(struct Node *head) { - //YourCode here - //Use fast and slow pointer technique + if (head == NULL) + return; + + struct Node* slow = head; + struct Node* fast = head; + + while (fast != NULL && fast->next != NULL) + { + slow = slow->next; + fast = fast->next->next; + } + + printf("Middle element is [%d]\n", slow->data); } // Function to add a new node diff --git a/Exercise_4.cpp b/Exercise_4.cpp index 1a528ee6..aecd04ef 100644 --- a/Exercise_4.cpp +++ b/Exercise_4.cpp @@ -6,14 +6,46 @@ // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { - //Your code here + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + + 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]; + + int i = 0, j = 0, k = l; + + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + arr[k++] = L[i++]; + else + arr[k++] = R[j++]; + } + + while (i < n1) + arr[k++] = L[i++]; + + while (j < n2) + arr[k++] = R[j++]; } /* l is for left index and r is right index of the sub-array of arr to be sorted */ void mergeSort(int arr[], int l, int r) { - //Your code here + if (l < r) + { + int m = l + (r - l) / 2; + + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } } /* UTILITY FUNCTIONS */ diff --git a/Exercise_5.cpp b/Exercise_5.cpp index a07c2bf6..f69bc920 100644 --- a/Exercise_5.cpp +++ b/Exercise_5.cpp @@ -1,3 +1,6 @@ +//Time Complexity : O(n logn) +// Space Complexity : O(logn) + #include using namespace std; @@ -12,7 +15,20 @@ void swap(int* a, int* b) /* This function is same in both iterative and recursive*/ int partition(int arr[], int l, int h) { - //Do the comparison and swapping here + int pivot = arr[h]; + int i = l - 1; + + for (int j = l; j <= h - 1; j++) + { + if (arr[j] <= pivot) + { + i++; + swap(&arr[i], &arr[j]); + } + } + + swap(&arr[i + 1], &arr[h]); + return i + 1; } /* A[] --> Array to be sorted, @@ -20,7 +36,35 @@ l --> Starting index, h --> Ending index */ void quickSortIterative(int arr[], int l, int h) { - //Try to think that how you can use stack here to remove recursion. + // Create an explicit stack + stack st; + + // Push initial values + st.push(l); + st.push(h); + + // Pop until stack is empty + while (!st.empty()) + { + h = st.top(); st.pop(); + l = st.top(); st.pop(); + + int p = partition(arr, l, h); + + // If elements on left side of pivot + if (p - 1 > l) + { + st.push(l); + st.push(p - 1); + } + + // If elements on right side of pivot + if (p + 1 < h) + { + st.push(p + 1); + st.push(h); + } + } } // A utility function to print contents of arr