-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: add generic find_max_min implementation with tests #2999
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
Open
SyedAli-Pk
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
SyedAli-Pk:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,84 @@ | ||
/** | ||
* @file | ||
* @brief Program to find maximum and minimum element in an array | ||
* | ||
* @details | ||
* This program demonstrates how to find the maximum and minimum values | ||
* in an array of elements. It is implemented as a template function so | ||
* it works with any comparable data type. | ||
* | ||
* The algorithm simply iterates through the array once, updating the | ||
* maximum and minimum values as needed. This results in \f$O(n)\f$ time | ||
* complexity and \f$O(1)\f$ space complexity. | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for std::cout, std::endl | ||
#include <vector> /// for std::vector | ||
#include <utility> /// for std::pair | ||
|
||
/** | ||
* @namespace arrays | ||
* @brief Array-based algorithms | ||
*/ | ||
namespace arrays { | ||
/** | ||
* @namespace find_max_min | ||
* @brief Functions for finding maximum and minimum values | ||
*/ | ||
namespace find_max_min { | ||
/** | ||
* @brief Find maximum and minimum in an array | ||
* @tparam T data type of elements (must support comparisons) | ||
* @param arr input array | ||
* @return std::pair<T, T> containing {max, min} | ||
*/ | ||
template <typename T> | ||
std::pair<T, T> findMaxMin(const std::vector<T>& arr) { | ||
assert(!arr.empty()); // array must not be empty | ||
T maxVal = arr[0]; | ||
T minVal = arr[0]; | ||
for (size_t i = 1; i < arr.size(); i++) { | ||
if (arr[i] > maxVal) { | ||
maxVal = arr[i]; | ||
} | ||
if (arr[i] < minVal) { | ||
minVal = arr[i]; | ||
} | ||
} | ||
return {maxVal, minVal}; | ||
} | ||
} // namespace find_max_min | ||
} // namespace arrays | ||
|
||
/** | ||
* @brief Self-test implementation | ||
*/ | ||
static void test() { | ||
using arrays::find_max_min::findMaxMin; | ||
|
||
std::vector<int> vec1 = {3, 1, 7, -5, 9}; | ||
auto result1 = findMaxMin(vec1); | ||
assert(result1.first == 9 && result1.second == -5); | ||
|
||
std::vector<float> vec2 = {1.5, 2.5, 0.5}; | ||
auto result2 = findMaxMin(vec2); | ||
assert(result2.first == 2.5f && result2.second == 0.5f); | ||
|
||
std::vector<char> vec3 = {'a', 'z', 'm'}; | ||
auto result3 = findMaxMin(vec3); | ||
assert(result3.first == 'z' && result3.second == 'a'); | ||
|
||
std::vector<int> vec4 = {42}; | ||
auto result4 = findMaxMin(vec4); | ||
assert(result4.first == 42 && result4.second == 42); | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
*/ | ||
int main() { | ||
test(); | ||
std::cout << "All tests passed!\n"; | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return an empty pair?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because, imagine a user gives an empty array they get an empty pair back, no max, no min. You could make it testable.