Skip to content

Commit

Permalink
feat: implementation of linear search
Browse files Browse the repository at this point in the history
  • Loading branch information
noctera committed Dec 18, 2021
1 parent a2c82df commit 0a057c1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
16 changes: 16 additions & 0 deletions include/AlgoCpp/algorithms/search/linearSearch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

namespace algocpp {
namespace search {

template <typename T, typename Z>
bool linearSearch(T& input, Z item) {
for (auto it = input.begin(); it != input.end(); ++it) {
if (*it == item) {
return true;
}
}
return false;
}
} // namespace search
} // namespace algocpp
3 changes: 1 addition & 2 deletions include/AlgoCpp/algorithms/sorting/bubbleSort.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <iostream>
#include <vector>
#include <algorithm>

namespace algocpp {
namespace sorting {
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_executable(
${PROJECT_NAME}
"main.cpp"
"sorting/bubbleSort.cpp"
"search/linearSearch.cpp"
${BenchmarkFiles}

)
Expand Down
21 changes: 21 additions & 0 deletions tests/search/linearSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include "AlgoCpp/algorithms/search/linearSearch.hpp"
#include <array>
#include <catch2/catch.hpp>
#include <iostream>
#include <vector>

using namespace algocpp::search;

TEST_CASE("Check if linear search is working", "[linearSearch]") {
std::vector<int> test1 = {1, 4, 5, 2, 3};
std::vector<int> test2 = {1};
std::vector<char> test3 = {'c', 'b', 'x', 'm', 'n'};

REQUIRE(linearSearch(test1, 5) == true);
REQUIRE(linearSearch(test1, 7) == false);
REQUIRE(linearSearch(test2, 1) == true);
REQUIRE(linearSearch(test2, 5) == false);
REQUIRE(linearSearch(test3, 'b') == true);
REQUIRE(linearSearch(test3, 'A') == false);
}

0 comments on commit 0a057c1

Please sign in to comment.