-
Notifications
You must be signed in to change notification settings - Fork 5
Add sorting functions that track number of swaps #15
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6fc5e6b
Add sorting functions that track number of swaps
hmenke f479ce2
Remove overloads for sorting functions using std::less
Thoemi09 f27a12c
Update sorting function docs for consistency
Thoemi09 756a862
Relax iterator requirements in insertion sort
Thoemi09 10ded7c
Update sorting tests
Thoemi09 48be308
Add range overload for sorting functions
Thoemi09 a5e3485
Tighten iterator requirements in insertion sort test
hmenke 240c3bf
Perform non-deterministic randomized testing for sorting
hmenke 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
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,140 @@ | ||
| // Copyright (c) 2024 Simons Foundation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0.txt | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell, chuffa | ||
|
|
||
| /** | ||
| * @file | ||
| * @brief Provides functions for sorting ranges. | ||
| */ | ||
|
|
||
| #ifndef _ITERTOOLS_SORT_HPP | ||
| #define _ITERTOOLS_SORT_HPP | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <functional> | ||
| #include <iterator> | ||
| #include <ranges> | ||
|
|
||
| namespace itertools { | ||
|
|
||
| /** | ||
| * @addtogroup sorting | ||
| * @{ | ||
| */ | ||
|
|
||
| /** | ||
| * @brief Bubble sort elements in the given range. | ||
| * | ||
| * @details Sort the elements in the range `[first, last)` in the order prescribed by the comparison function `comp`. | ||
| * The underlying sorting algorithm is a stable bubble sort, i.e. already sorted elements will not be swapped. The | ||
| * number of swaps necessary to get the elements into sorted order is recorded and returned. | ||
| * | ||
| * Computational complexity: \f$ \mathcal{O}(n^2) \f$. | ||
| * | ||
| * This function is eager and puts the range in sorted order. | ||
| * | ||
| * @tparam ForwardIt Forward iterator type. | ||
| * @tparam Compare Comparison function type. | ||
| * @param first Forward iterator to the first element of the range. | ||
| * @param last Forward iterator to the element after the last of the range. | ||
| * @param comp Comparison function callable with two dereferenced iterators. | ||
| * @return Number of swaps necessary to sort the range. | ||
| */ | ||
| template <std::forward_iterator ForwardIt, class Compare = std::less<>> | ||
| std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp = {}) { | ||
| if (first == last) { return 0; } | ||
| std::size_t n_swaps = 0; | ||
| for (ForwardIt sorted = first; first != last; last = sorted) { | ||
| sorted = first; | ||
| for (ForwardIt curr = first, prev = first; ++curr != last; ++prev) { | ||
| if (comp(*curr, *prev)) { | ||
| std::iter_swap(curr, prev); | ||
| sorted = curr; | ||
| ++n_swaps; | ||
| } | ||
| } | ||
| } | ||
| return n_swaps; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Insertion sort elements in the given range. | ||
| * | ||
| * @details Sort the elements in the range `[first, last)` in the order prescribed by the comparison function `comp`. | ||
| * The underlying sorting algorithm is a stable insertion sort, i.e. already sorted elements will not be swapped. The | ||
| * number of swaps necessary to get the elements into sorted order is recorded and returned. | ||
| * | ||
| * Computational complexity: \f$ \mathcal{O}(n^2) \f$. | ||
| * | ||
| * This function is eager and puts the range in sorted order. | ||
| * | ||
| * @tparam BidirIt Bidirectional iterator type. | ||
| * @tparam Compare Comparison function type. | ||
| * @param first Bidirectional iterator to the first element of the range. | ||
| * @param last Bidirectional iterator to the element after the last of the range. | ||
| * @param comp Comparison function callable with two dereferenced iterators. | ||
| * @return Number of swaps necessary to sort the range. | ||
| */ | ||
| template <std::bidirectional_iterator BidirIt, class Compare = std::less<>> | ||
| std::size_t insertion_sort(BidirIt first, BidirIt last, Compare comp = {}) { | ||
| if (first == last) { return 0; } | ||
| std::size_t swaps = 0; | ||
| for (BidirIt i = std::next(first); i != last; ++i) { | ||
| for (BidirIt j = i; j != first && comp(*j, *std::prev(j)); --j) { | ||
| std::iter_swap(std::prev(j), j); | ||
| ++swaps; | ||
| } | ||
| } | ||
| return swaps; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Bubble sort elements in the given range. | ||
| * | ||
| * @details See itertools::bubble_sort for more details. | ||
| * | ||
| * @tparam Range Forward range type. | ||
| * @tparam Compare Comparison function type. | ||
| * @param rng A forward range to sort. | ||
| * @param comp Comparison function callable with two dereferenced iterators. | ||
| * @return Number of swaps necessary to sort the range. | ||
| */ | ||
| template <std::ranges::forward_range Range, class Compare = std::less<>> | ||
| std::size_t bubble_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded) | ||
| return bubble_sort(std::ranges::begin(rng), std::ranges::end(rng), comp); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Insertion sort elements in the given range. | ||
| * | ||
| * @details See itertools::insertion_sort for more details. | ||
| * | ||
| * @tparam Range Bidirectional range type. | ||
| * @tparam Compare Comparison function type. | ||
| * @param rng A bidirectional range to sort. | ||
| * @param comp Comparison function callable with two dereferenced iterators. | ||
| * @return Number of swaps necessary to sort the range. | ||
| */ | ||
| template <std::ranges::bidirectional_range Range, class Compare = std::less<>> | ||
| std::size_t insertion_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded) | ||
| return insertion_sort(std::ranges::begin(rng), std::ranges::end(rng), comp); | ||
| } | ||
|
|
||
| /** @} */ | ||
|
|
||
| } // namespace itertools | ||
|
|
||
| #endif // _ITERTOOLS_SORT_HPP |
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
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
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
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
Oops, something went wrong.
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.
@Thoemi09 I want to do completely non-deterministic randomized testing here, in particular on the length of the container. The first bug that I found when I wrote the test was that my functions didn't correctly sort the empty range. Even better would be fuzzing but I don't think TRIQS has any setup for that.