-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Legoeggolas/main
Add a C++ implementation of merge sort
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 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,95 @@ | ||
#include "msort.hpp" | ||
|
||
using vecIter = std::vector<int>::iterator; | ||
|
||
void merge(vecIter beginFirst, | ||
vecIter endFirst, | ||
vecIter beginSecond, | ||
vecIter endSecond) { | ||
auto const begin = beginFirst; | ||
|
||
std::vector<int> temp(std::distance(beginFirst, endSecond)); | ||
auto it = temp.begin(); | ||
|
||
while (beginFirst != endFirst && beginSecond != endSecond) { | ||
if (*beginFirst <= *beginSecond) { | ||
*it = *beginFirst; | ||
beginFirst++; | ||
} else { | ||
*it = *beginSecond; | ||
beginSecond++; | ||
} | ||
|
||
it++; | ||
} | ||
|
||
while (beginFirst != endFirst) { | ||
*it = *beginFirst; | ||
it++; | ||
beginFirst++; | ||
} | ||
|
||
while (beginSecond != endSecond) { | ||
*it = *beginSecond; | ||
it++; | ||
beginSecond++; | ||
} | ||
|
||
it = temp.begin(); | ||
beginFirst = begin; | ||
for (; beginFirst != endSecond; beginFirst++, it++) { | ||
*beginFirst = *it; | ||
} | ||
} | ||
|
||
void msort(vecIter begin, vecIter end) { | ||
size_t len = std::distance(begin, end); | ||
if (len <= 1) { | ||
return; | ||
} | ||
|
||
auto mid = begin + (len / 2); | ||
|
||
msort(begin, mid); | ||
msort(mid, end); | ||
|
||
merge(begin, mid, mid, end); | ||
} | ||
|
||
/* | ||
bool isSorted(vecIter begin, vecIter end) { | ||
for (auto hare = begin + 1; hare != end; hare++, begin++) { | ||
if (*hare < *begin) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
int main() { | ||
std::vector<int> vec(100000); | ||
// First create an instance of an engine. | ||
std::random_device rnd_device; | ||
// Specify the engine and distribution. | ||
std::mt19937 mersenne_engine{rnd_device()}; // Generates random integers | ||
std::uniform_int_distribution<int> dist{1, 1024}; | ||
auto gen = [&dist, &mersenne_engine]() { | ||
return dist(mersenne_engine); | ||
}; | ||
std::generate(std::begin(vec), std::end(vec), gen); | ||
assert(isSorted(vec.begin(), vec.end()) == false); | ||
msort(vec.begin(), vec.end()); | ||
assert(isSorted(vec.begin(), vec.end())); | ||
std::cout << "Done"; | ||
return 0; | ||
} | ||
*/ |
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,20 @@ | ||
#ifndef M_SORT_H | ||
#define M_SORT_H | ||
|
||
//#include <assert.h> | ||
|
||
#include <algorithm> | ||
//#include <iostream> | ||
#include <iterator> | ||
//#include <random> | ||
#include <vector> | ||
|
||
/** | ||
* @brief Performs merge sort on a range defined by two iterators. | ||
* | ||
* @param begin The start of the range. | ||
* @param end The end of the range. | ||
*/ | ||
void msort(std::vector<int>::iterator begin, std::vector<int>::iterator end); | ||
|
||
#endif |