Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KMP Search Algorithm #2821

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions search/Knuth-Morris-Pratt (KMP) Algorithm for Pattern Searching
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <bits/stdc++.h>
dev-priyanshu15 marked this conversation as resolved.
Show resolved Hide resolved
using namespace std;

vector<int> computeLPSArray(string& pattern) {
int m = pattern.size();
vector<int> 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<int> 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;
}
37 changes: 37 additions & 0 deletions search/Quickselect for Finding the k-th Smallest Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
using namespace std;

int partition(vector<int>& 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<int>& 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<int> 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;
}
35 changes: 35 additions & 0 deletions search/Staircase Search in a 2D Matrix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;

bool staircaseSearch(vector<vector<int>>& 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<vector<int>> 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;
}