-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implementation of linear search
- Loading branch information
noctera
committed
Dec 18, 2021
1 parent
a2c82df
commit 0a057c1
Showing
4 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |