diff --git a/Coding/C++/median of two sorted arrays.cpp b/Coding/C++/median of two sorted arrays.cpp new file mode 100644 index 00000000..4e70f97c --- /dev/null +++ b/Coding/C++/median of two sorted arrays.cpp @@ -0,0 +1,81 @@ +// A Simple Merge based O(n) +// solution to find median of +// two sorted arrays +#include +using namespace std; + +/* This function returns +median of ar1[] and ar2[]. +Assumptions in this function: +Both ar1[] and ar2[] +are sorted arrays +Both have n elements */ +double getMedian(int ar1[], int ar2[], int n) +{ + int i = 0; /* Current index of + i/p array ar1[] */ + int j = 0; /* Current index of + i/p array ar2[] */ + int count; + int m1 = -1, m2 = -1; + + /* Since there are 2n elements, + median will be average of elements + at index n-1 and n in the array + obtained after merging ar1 and ar2 */ + for (count = 0; count <= n; count++) { + /* Below is to handle case where + all elements of ar1[] are + smaller than smallest(or first) + element of ar2[]*/ + if (i == n) { + m1 = m2; + m2 = ar2[0]; + break; + } + + /*Below is to handle case where + all elements of ar2[] are + smaller than smallest(or first) + element of ar1[]*/ + else if (j == n) { + m1 = m2; + m2 = ar1[0]; + break; + } + /* equals sign because if two + arrays have some common elements */ + if (ar1[i] <= ar2[j]) { + /* Store the prev median */ + m1 = m2; + m2 = ar1[i]; + i++; + } + else { + /* Store the prev median */ + m1 = m2; + m2 = ar2[j]; + j++; + } + } + + return (1.0 * (m1 + m2)) / 2; +} + +// Driver Code +int main() +{ + int ar1[] = {1, 12, 15, 26, 38}; + int ar2[] = {2, 13, 17, 30, 45}; + + int n1 = sizeof(ar1) / sizeof(ar1[0]); + int n2 = sizeof(ar2) / sizeof(ar2[0]); + if (n1 == n2) + cout << "Median is " << getMedian(ar1, ar2, n1); + else + cout << "Doesn't work for arrays" + << " of unequal size"; + getchar(); + return 0; +} + diff --git a/Coding/C++/quick sort.cpp b/Coding/C++/quick sort.cpp new file mode 100644 index 00000000..aa61746d --- /dev/null +++ b/Coding/C++/quick sort.cpp @@ -0,0 +1,71 @@ +// C++ Implementation of the Quick Sort Algorithm. +#include +using namespace std; + +int partition(int arr[], int start, int end) +{ + + int pivot = arr[start]; + + int count = 0; + for (int i = start + 1; i <= end; i++) { + if (arr[i] <= pivot) + count++; + } + + // Giving pivot element its correct position + int pivotIndex = start + count; + swap(arr[pivotIndex], arr[start]); + + // Sorting left and right parts of the pivot element + int i = start, j = end; + + while (i < pivotIndex && j > pivotIndex) { + + while (arr[i] <= pivot) { + i++; + } + + while (arr[j] > pivot) { + j--; + } + + if (i < pivotIndex && j > pivotIndex) { + swap(arr[i++], arr[j--]); + } + } + + return pivotIndex; +} + +void quickSort(int arr[], int start, int end) +{ + + // base case + if (start >= end) + return; + + // partitioning the array + int p = partition(arr, start, end); + + // Sorting the left part + quickSort(arr, start, p - 1); + + // Sorting the right part + quickSort(arr, p + 1, end); +} + +int main() +{ + + int arr[] = { 9, 3, 4, 2, 1, 8 }; + int n = 6; + + quickSort(arr, 0, n - 1); + + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + + return 0; +} diff --git a/Coding/C++/rotated array.cpp b/Coding/C++/rotated array.cpp new file mode 100644 index 00000000..3d53f7fa --- /dev/null +++ b/Coding/C++/rotated array.cpp @@ -0,0 +1,82 @@ +// C++ Program to search an element +// in a sorted and pivoted array + +#include +using namespace std; + +// Standard Binary Search function +int binarySearch(int arr[], int low, int high, int key) +{ + if (high < low) + return -1; + + int mid = (low + high) / 2; + if (key == arr[mid]) + return mid; + + if (key > arr[mid]) + return binarySearch(arr, (mid + 1), high, key); + + return binarySearch(arr, low, (mid - 1), key); +} + +// Function to get pivot. For array 3, 4, 5, 6, 1, 2 +// it returns 3 (index of 6) +int findPivot(int arr[], int low, int high) +{ + // Base cases + if (high < low) + return -1; + if (high == low) + return low; + + // low + (high - low)/2; + int mid = (low + high) / 2; + if (mid < high && arr[mid] > arr[mid + 1]) + return mid; + + if (mid > low && arr[mid] < arr[mid - 1]) + return (mid - 1); + + if (arr[low] >= arr[mid]) + return findPivot(arr, low, mid - 1); + + return findPivot(arr, mid + 1, high); +} + +// Searches an element key in a pivoted +// sorted array arr[] of size n +int pivotedBinarySearch(int arr[], int n, int key) +{ + int pivot = findPivot(arr, 0, n - 1); + + // If we didn't find a pivot, + // then array is not rotated at all + if (pivot == -1) + return binarySearch(arr, 0, n - 1, key); + + // If we found a pivot, then first compare with pivot + // and then search in two subarrays around pivot + if (arr[pivot] == key) + return pivot; + + if (arr[0] <= key) + return binarySearch(arr, 0, pivot - 1, key); + + return binarySearch(arr, pivot + 1, n - 1, key); +} + +// Driver program to check above functions +int main() +{ + // Let us search 3 in below array + int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 }; + int n = sizeof(arr1) / sizeof(arr1[0]); + int key = 3; + + // Function calling + cout << "Index of the element is : " + << pivotedBinarySearch(arr1, n, key); + + return 0; +} \ No newline at end of file