Skip to content

Commit

Permalink
add basic hash and some use case for search implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyhuynh committed Nov 7, 2024
1 parent f8f45a5 commit 551dd67
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
54 changes: 54 additions & 0 deletions DataStructure/11_SortingAndSearch/BasicHashSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <string>
#include <vector>

using namespace std;

unsigned long simpleHash(const string& input) {
unsigned long hash = 0;
for(char c : input) {
hash = hash * 31 + c;
}
return hash%10;
}

bool isContained (const vector<string>& dataArr, string data){
if (dataArr[simpleHash(data)]==data){
return true;
}
return false;
}

int main() {
vector<string> dataArr(10);
for (int i = 0; i < 10; ++i) {
dataArr[i] = " string ";
}
for (int i = 0; i < 10; ++i){
cout << dataArr[i] << ", ";
}
cout << endl;

string data = "Hello, world!";
unsigned long hashValue = simpleHash(data);
cout << "Hash Value: " << hashValue << endl;

dataArr[hashValue] = data;

for (int i = 0; i < 10; ++i){
cout << dataArr[i] << ", ";
}
cout << endl;

bool option = isContained(dataArr,data);

if (option){
cout << "data is contained";
} else {
cout << "not found";
}

return 0;
}

// Use case is Search for "Hello World" in side the data set
4 changes: 2 additions & 2 deletions DataStructure/11_SortingAndSearch/SearchImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ int binarySearch(const std::vector<int>& arr, int x) {

int main() {
// Creating an array of 200 elements
std::vector<int> data(200);
for (int i = 0; i < 200; ++i) {
std::vector<int> data(20000);
for (int i = 0; i < 20000; ++i) {
data[i] = i;
}

Expand Down

0 comments on commit 551dd67

Please sign in to comment.