diff --git a/day29/C++/binary_search b/day29/C++/binary_search new file mode 100755 index 00000000..a71eb46b Binary files /dev/null and b/day29/C++/binary_search differ diff --git a/day29/C++/binary_search.cpp b/day29/C++/binary_search.cpp new file mode 100644 index 00000000..6feeed3b --- /dev/null +++ b/day29/C++/binary_search.cpp @@ -0,0 +1,55 @@ +/* +* @author : imkaka +* @date : 31/1/2019 +*/ + +#include +#include + +using namespace std; + +int binary_search_itr(int arr[], int size, int val){ + int l = 0, r = size-1; + int mid = (l + r) / 2; + + while(arr[mid] != val && l <= r){ + if(val < arr[mid]){ + r = mid - 1; + } + else{ + l = mid +1; + } + + mid = (l + r) / 2; + } + + if(arr[mid] == val) + return mid; + + return -1; +} + +int binary_search_rec(int arr[], int left, int right, int val){ + if(right >= left){ + + int mid = left + (right - left) / 2; + + if(arr[mid] == val) return mid; + + if(arr[mid] > val) + return binary_search_rec(arr, left, mid-1, val); + return binary_search_rec(arr, mid+1, right, val); + } + + return -1; +} + +int main(){ + + int arr[] = {1, 2, 3, 6, 9, 15, 16, 14}; + int val = 9; + int size = sizeof(arr)/sizeof(arr[0]); + cout << "Iterative: " << binary_search_itr(arr, size, val) << endl; + cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl; + return 0; +} diff --git a/day29/README.md b/day29/README.md index 32bbb1f8..38861fb9 100644 --- a/day29/README.md +++ b/day29/README.md @@ -41,5 +41,69 @@ function binary (arr, n) { return -1; } -console.log (binary ([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)); // 4 -``` \ No newline at end of file +console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8)); +console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7)); +``` + +## C++ Implementation + +### [Solution by @imkaka](./C++/binary_search.cpp) + +```cpp + +/* +* @author : imkaka +* @date : 31/1/2019 +*/ + +#include +#include + +using namespace std; + +int binary_search_itr(int arr[], int size, int val){ + int l = 0, r = size-1; + int mid = (l + r) / 2; + + while(arr[mid] != val && l <= r){ + if(val < arr[mid]){ + r = mid - 1; + } + else{ + l = mid +1; + } + + mid = (l + r) / 2; + } + + if(arr[mid] == val) + return mid; + + return -1; +} + +int binary_search_rec(int arr[], int left, int right, int val){ + if(right >= left){ + + int mid = left + (right - left) / 2; + + if(arr[mid] == val) return mid; + + if(arr[mid] > val) + return binary_search_rec(arr, left, mid-1, val); + return binary_search_rec(arr, mid+1, right, val); + } + + return -1; +} + +int main(){ + + int arr[] = {1, 2, 3, 6, 9, 15, 16, 14}; + int val = 9; + int size = sizeof(arr)/sizeof(arr[0]); + cout << "Iterative: " << binary_search_itr(arr, size, val) << endl; + cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl; + return 0; +} +``` diff --git a/day30/C++/naiveSearch b/day30/C++/naiveSearch new file mode 100755 index 00000000..29961fab Binary files /dev/null and b/day30/C++/naiveSearch differ diff --git a/day30/C++/naiveSearch.cpp b/day30/C++/naiveSearch.cpp new file mode 100644 index 00000000..494ea824 --- /dev/null +++ b/day30/C++/naiveSearch.cpp @@ -0,0 +1,78 @@ +/* +* @author : imkaka +* @date : 1/2/2019 +*/ + +#include +#include + +using namespace std; + +void computeLPS(string, int, int []); + +// KMP Algorithm +void KMPsearch(string text, string pat){ + // Length + int N = text.size(); + int M = pat.size(); + + // Define LPS (Longest Proper Prefix) + int lps[M]; + + //Preprocess + computeLPS(pat, M, lps); + + int i = 0, j = 0; + while(i < N){ + //While Match + if(pat[j] == text[i]){ + i++; + j++; + } + + if(j == M){ + cout << "Pattern Found At " << (i-j) << endl; + j = lps[j-1]; + } + + else if(i < N && pat[j] != text[i]){ + if(lps[j] != 0) + j = lps[j-1]; + else + i++; + } + } +} + +void computeLPS(string pat, int M, int lps[]){ + int len = 0; //Track len of longest common prefix which is suffix also. + + lps[0] = 0; + int i = 1; + + while(i < M){ + if(pat[i] == pat[len]){ + len++; + lps[i] = len; + i++; + } + else{ + if(len != 0){ + len = lps[len-1]; //Don't increment i + } + else{ + lps[i] = 0; + i++; + } + } + } +} + +int main(){ + + string txt = "ABABDABACDABABCABAB"; + string pat = "ABABCABAB"; + + KMPsearch(txt, pat); + return 0; +} diff --git a/day30/README.md b/day30/README.md index 8e18877d..83d27edd 100644 --- a/day30/README.md +++ b/day30/README.md @@ -11,7 +11,7 @@ Given a sentence (string), and a pattern, write a function that returns the inde **Example** ``` -input: +input: str = "Hello World, Coding is beautiful" pattern = "World" @@ -26,4 +26,87 @@ output: 6 (start index of the found pattern) ```js to be added -``` \ No newline at end of file +``` + +### [C++ Implementation](./C++/naiveSearch.cpp) + +```cpp +/* +* @author : imkaka +* @date : 1/2/2019 +*/ + +#include +#include + +using namespace std; + +void computeLPS(string, int, int []); + +// KMP Algorithm +void KMPsearch(string text, string pat){ + // Length + int N = text.size(); + int M = pat.size(); + + // Define LPS (Longest Proper Prefix) + int lps[M]; + + //Preprocess + computeLPS(pat, M, lps); + + int i = 0, j = 0; + while(i < N){ + //While Match + if(pat[j] == text[i]){ + i++; + j++; + } + + if(j == M){ + cout << "Pattern Found At " << (i-j) << endl; + j = lps[j-1]; + } + + else if(i < N && pat[j] != text[i]){ + if(lps[j] != 0) + j = lps[j-1]; + else + i++; + } + } +} + +void computeLPS(string pat, int M, int lps[]){ + int len = 0; //Track len of longest common prefix which is suffix also. + + lps[0] = 0; + int i = 1; + + while(i < M){ + if(pat[i] == pat[len]){ + len++; + lps[i] = len; + i++; + } + else{ + if(len != 0){ + len = lps[len-1]; //Don't increment i + } + else{ + lps[i] = 0; + i++; + } + } + } +} + +int main(){ + + string txt = "ABABDABACDABABCABAB"; + string pat = "ABABCABAB"; + + KMPsearch(txt, pat); + return 0; +} +``` diff --git a/day31/C++/bubbleSort b/day31/C++/bubbleSort new file mode 100755 index 00000000..ba1521d1 Binary files /dev/null and b/day31/C++/bubbleSort differ diff --git a/day31/C++/bubbleSort.cpp b/day31/C++/bubbleSort.cpp new file mode 100644 index 00000000..fa593576 --- /dev/null +++ b/day31/C++/bubbleSort.cpp @@ -0,0 +1,53 @@ +/* +* @author : imkaka +* @date : 1/2/2019 +* +*/ + +#include +#include + +using namespace std; + +void bubblesort(vector& arr){ + int N = arr.size(); + + bool swapped; + for(int i = 0; i < N-1; ++i){ + swapped = false; // Optimizing part + for(int j = 0; j < N-i-1; ++j) // (j, j+1) Adjacent Swap. + + if(arr[j] > arr[j+1]){ // Swap Part + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + swapped = true; + } + if(!swapped) + break; + } +} + +void printArray(const vector &arr){ + int N = arr.size(); + + for(const int x : arr){ + cout << x << " "; + } + cout << endl; +} + +int main(){ + + vector arr = {20, 10, 0, 36, 100, 12, -20, 30, 50, -100}; + + cout << "Unsorted:" << endl; + printArray(arr); + + bubblesort(arr); + + cout << "Sorted: " << endl; + printArray(arr); + return 0; +} + diff --git a/day31/README.md b/day31/README.md index f18c100c..1ae9134a 100644 --- a/day31/README.md +++ b/day31/README.md @@ -25,4 +25,62 @@ output: [1, 2, 3, 4, 5, 8, 9] ```js to be added -``` \ No newline at end of file +``` + +### [C++ Implementation](./C++/bubbleSort.cpp) + +```cpp + +/* +* @author : imkaka +* @date : 1/2/2019 +* +*/ + +#include +#include + +using namespace std; + +void bubblesort(vector& arr){ + int N = arr.size(); + + bool swapped; + for(int i = 0; i < N-1; ++i){ + swapped = false; // Optimizing part + for(int j = 0; j < N-i-1; ++j) // (j, j+1) Adjacent Swap. + + if(arr[j] > arr[j+1]){ // Swap Part + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + swapped = true; + } + if(!swapped) + break; + } +} + +void printArray(const vector &arr){ + int N = arr.size(); + + for(const int x : arr){ + cout << x << " "; + } + cout << endl; +} + +int main(){ + + vector arr = {20, 10, 0, 36, 100, 12, -20, 30, 50, -100}; + + cout << "Unsorted:" << endl; + printArray(arr); + + bubblesort(arr); + + cout << "Sorted: " << endl; + printArray(arr); + return 0; +} +```