-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
minor readme change
1 parent
c2a66f5
commit aaa0c00
Showing
2 changed files
with
21 additions
and
18 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
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,27 +1,28 @@ | ||
#ifndef ALGOPLUS_MERGE_SORT_H | ||
#define ALGOPLUS_MERGE_SORT_H | ||
#ifndef MERGE_SORT_H | ||
#define MERGE_SORT_H | ||
|
||
#include <algorithm> // Required for std::inplace_merge | ||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> // Required for std::inplace_merge | ||
|
||
/** | ||
* @brief Performs a merge sort on a range of elements. | ||
* @param begin An iterator to the beginning of the range to be sorted. | ||
* @param end An iterator to the end of the range to be sorted. | ||
* @details This function sorts the elements in the range [begin, end) into ascending order. | ||
* The sort is performed in-place. It divides the range into two halves, recursively sorts each half, | ||
* and then merges the two sorted halves together. The function requires that the range of elements | ||
* is sortable by the criteria used in std::inplace_merge. | ||
* @details This function sorts the elements in the range [begin, end) into | ||
* ascending order. The sort is performed in-place. It divides the range into | ||
* two halves, recursively sorts each half, and then merges the two sorted | ||
* halves together. The function requires that the range of elements is sortable | ||
* by the criteria used in std::inplace_merge. | ||
*/ | ||
template <typename Iter> | ||
void merge_sort(Iter begin, Iter end) { | ||
if (end - begin > 1) { | ||
Iter mid = begin + (end - begin) / 2; // Assumes Random Access Iterator | ||
merge_sort(begin, mid); | ||
merge_sort(mid, end); | ||
std::inplace_merge(begin, mid, end); // Elements must be comparable and move-assignable | ||
} | ||
template <typename Iter> void merge_sort(Iter begin, Iter end) { | ||
if (end - begin > 1) { | ||
Iter mid = begin + (end - begin) / 2; // Assumes Random Access Iterator | ||
merge_sort(begin, mid); | ||
merge_sort(mid, end); | ||
std::inplace_merge(begin, mid, | ||
end); // Elements must be comparable and move-assignable | ||
} | ||
} | ||
|
||
#endif //ALGOPLUS_MERGE_SORT_H | ||
#endif // MERGE_SORT_H |