From 62eef0886f5eb2937db8cac775f2ab637ee55e85 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Sat, 12 Oct 2024 15:14:38 +0530 Subject: [PATCH 1/6] Create Staircase Search in a 2D Matrix --- search/Staircase Search in a 2D Matrix | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 search/Staircase Search in a 2D Matrix diff --git a/search/Staircase Search in a 2D Matrix b/search/Staircase Search in a 2D Matrix new file mode 100644 index 00000000000..1b1193caad1 --- /dev/null +++ b/search/Staircase Search in a 2D Matrix @@ -0,0 +1,35 @@ +#include +using namespace std; + +bool staircaseSearch(vector>& matrix, int target) { + int rows = matrix.size(); + int cols = matrix[0].size(); + int row = 0, col = cols - 1; + + while (row < rows && col >= 0) { + if (matrix[row][col] == target) { + return true; + } else if (matrix[row][col] > target) { + col--; + } else { + row++; + } + } + return false; +} + +int main() { + vector> matrix = { + {1, 4, 7, 11}, + {2, 5, 8, 12}, + {3, 6, 9, 16}, + {10, 13, 14, 17} + }; + int target = 8; + if (staircaseSearch(matrix, target)) + cout << "Element found." << endl; + else + cout << "Element not found." << endl; + + return 0; +} From 598a6e4fd8e63ff46b408e01a179aa80c9753d3f Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Sat, 12 Oct 2024 15:20:03 +0530 Subject: [PATCH 2/6] Create Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching --- ...ratt (KMP) Algorithm for Pattern Searching | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching diff --git a/search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching b/search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching new file mode 100644 index 00000000000..c4bdacc1ecf --- /dev/null +++ b/search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching @@ -0,0 +1,57 @@ +#include +using namespace std; + +vector computeLPSArray(string& pattern) { + int m = pattern.size(); + vector lps(m); + int len = 0; + int i = 1; + + while (i < m) { + if (pattern[i] == pattern[len]) { + len++; + lps[i] = len; + i++; + } else { + if (len != 0) { + len = lps[len - 1]; + } else { + lps[i] = 0; + i++; + } + } + } + return lps; +} + +void KMP(string& text, string& pattern) { + int n = text.size(); + int m = pattern.size(); + vector lps = computeLPSArray(pattern); + int i = 0, j = 0; + + while (i < n) { + if (pattern[j] == text[i]) { + i++; + j++; + } + + if (j == m) { + cout << "Pattern found at index " << i - j << endl; + j = lps[j - 1]; + } else if (i < n && pattern[j] != text[i]) { + if (j != 0) { + j = lps[j - 1]; + } else { + i++; + } + } + } +} + +int main() { + string text = "ababcabcabababd"; + string pattern = "ababd"; + KMP(text, pattern); + return 0; +} From 7af5bfb7bd8e5dfef59111ab534fc8db92569401 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Sat, 12 Oct 2024 15:21:32 +0530 Subject: [PATCH 3/6] Create Quickselect for Finding the k-th Smallest Element.cpp --- ... for Finding the k-th Smallest Element.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 search/Quickselect for Finding the k-th Smallest Element.cpp diff --git a/search/Quickselect for Finding the k-th Smallest Element.cpp b/search/Quickselect for Finding the k-th Smallest Element.cpp new file mode 100644 index 00000000000..0440f6ab188 --- /dev/null +++ b/search/Quickselect for Finding the k-th Smallest Element.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +int partition(vector& arr, int left, int right) { + int pivot = arr[right]; + int i = left; + + for (int j = left; j < right; ++j) { + if (arr[j] <= pivot) { + swap(arr[i], arr[j]); + i++; + } + } + swap(arr[i], arr[right]); + return i; +} + +int quickSelect(vector& arr, int left, int right, int k) { + if (left == right) return arr[left]; + + int pivotIndex = partition(arr, left, right); + + if (k == pivotIndex) { + return arr[k]; + } else if (k < pivotIndex) { + return quickSelect(arr, left, pivotIndex - 1, k); + } else { + return quickSelect(arr, pivotIndex + 1, right, k); + } +} + +int main() { + vector arr = {3, 2, 1, 5, 6, 4}; + int k = 2; + cout << k << "-th smallest element is " << quickSelect(arr, 0, arr.size() - 1, k - 1) << endl; + return 0; +} From 7be0b8f409ebc75bce1d0c9247cceaf9cf9e1ea7 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Tue, 15 Oct 2024 19:57:57 +0530 Subject: [PATCH 4/6] Update and rename Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching to KMPAlgorithm.cpp fix: Update KMP algorithm implementation to follow best practices - Changed file extension to .cpp for proper C++ recognition. - Removed usage of non-portable header. - Removed 'using namespace std;' and prefixed standard library components with std::. - Ensured code portability and compliance with repository standards. --- ...for Pattern Searching => KMPAlgorithm.cpp} | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) rename search/{Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching => KMPAlgorithm.cpp} (67%) diff --git a/search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching b/search/KMPAlgorithm.cpp similarity index 67% rename from search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching rename to search/KMPAlgorithm.cpp index c4bdacc1ecf..2845ee00811 100644 --- a/search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching +++ b/search/KMPAlgorithm.cpp @@ -1,9 +1,10 @@ -#include -using namespace std; +#include +#include +#include -vector computeLPSArray(string& pattern) { +std::vector computeLPSArray(const std::string& pattern) { int m = pattern.size(); - vector lps(m); + std::vector lps(m); int len = 0; int i = 1; @@ -24,10 +25,10 @@ vector computeLPSArray(string& pattern) { return lps; } -void KMP(string& text, string& pattern) { +void KMP(const std::string& text, const std::string& pattern) { int n = text.size(); int m = pattern.size(); - vector lps = computeLPSArray(pattern); + std::vector lps = computeLPSArray(pattern); int i = 0, j = 0; while (i < n) { @@ -37,7 +38,7 @@ void KMP(string& text, string& pattern) { } if (j == m) { - cout << "Pattern found at index " << i - j << endl; + std::cout << "Pattern found at index " << i - j << std::endl; j = lps[j - 1]; } else if (i < n && pattern[j] != text[i]) { if (j != 0) { @@ -50,8 +51,8 @@ void KMP(string& text, string& pattern) { } int main() { - string text = "ababcabcabababd"; - string pattern = "ababd"; + std::string text = "ababcabcabababd"; + std::string pattern = "ababd"; KMP(text, pattern); return 0; } From 5539bafab94107930260595bdfdf9d58a48efe0c Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:05:39 +0530 Subject: [PATCH 5/6] Delete search/Quickselect for Finding the k-th Smallest Element.cpp for 1 file i had deleted Quickselect for Finding the k-th Smallest Element.cpp will add after my review --- ... for Finding the k-th Smallest Element.cpp | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 search/Quickselect for Finding the k-th Smallest Element.cpp diff --git a/search/Quickselect for Finding the k-th Smallest Element.cpp b/search/Quickselect for Finding the k-th Smallest Element.cpp deleted file mode 100644 index 0440f6ab188..00000000000 --- a/search/Quickselect for Finding the k-th Smallest Element.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -using namespace std; - -int partition(vector& arr, int left, int right) { - int pivot = arr[right]; - int i = left; - - for (int j = left; j < right; ++j) { - if (arr[j] <= pivot) { - swap(arr[i], arr[j]); - i++; - } - } - swap(arr[i], arr[right]); - return i; -} - -int quickSelect(vector& arr, int left, int right, int k) { - if (left == right) return arr[left]; - - int pivotIndex = partition(arr, left, right); - - if (k == pivotIndex) { - return arr[k]; - } else if (k < pivotIndex) { - return quickSelect(arr, left, pivotIndex - 1, k); - } else { - return quickSelect(arr, pivotIndex + 1, right, k); - } -} - -int main() { - vector arr = {3, 2, 1, 5, 6, 4}; - int k = 2; - cout << k << "-th smallest element is " << quickSelect(arr, 0, arr.size() - 1, k - 1) << endl; - return 0; -} From fd2b962e7492ee98a0fab5687a421c1176743556 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:06:33 +0530 Subject: [PATCH 6/6] Delete search/Staircase Search in a 2D Matrix deleted as 1 file needed --- search/Staircase Search in a 2D Matrix | 35 -------------------------- 1 file changed, 35 deletions(-) delete mode 100644 search/Staircase Search in a 2D Matrix diff --git a/search/Staircase Search in a 2D Matrix b/search/Staircase Search in a 2D Matrix deleted file mode 100644 index 1b1193caad1..00000000000 --- a/search/Staircase Search in a 2D Matrix +++ /dev/null @@ -1,35 +0,0 @@ -#include -using namespace std; - -bool staircaseSearch(vector>& matrix, int target) { - int rows = matrix.size(); - int cols = matrix[0].size(); - int row = 0, col = cols - 1; - - while (row < rows && col >= 0) { - if (matrix[row][col] == target) { - return true; - } else if (matrix[row][col] > target) { - col--; - } else { - row++; - } - } - return false; -} - -int main() { - vector> matrix = { - {1, 4, 7, 11}, - {2, 5, 8, 12}, - {3, 6, 9, 16}, - {10, 13, 14, 17} - }; - int target = 8; - if (staircaseSearch(matrix, target)) - cout << "Element found." << endl; - else - cout << "Element not found." << endl; - - return 0; -}