mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-13 17:50:45 +08:00
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
#include <cstddef> // for size_t
|
||||
#include <utility> // for std::move & std::remove_reference_t
|
||||
template<class Iterator>
|
||||
template <class Iterator>
|
||||
void merge(Iterator, Iterator, const Iterator, char[]);
|
||||
/// bottom-up merge sort which sorts elements in a non-decreasing order
|
||||
/**
|
||||
@@ -17,13 +17,13 @@ void merge(Iterator, Iterator, const Iterator, char[]);
|
||||
* @param first points to the first element
|
||||
* @param last points to 1-step past the last element
|
||||
* @param n the number of elements
|
||||
*/
|
||||
template<class Iterator>
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
const size_t n) {
|
||||
// create a buffer large enough to store all elements
|
||||
// dynamically allocated to comply with cpplint
|
||||
char * buffer = new char[n * sizeof(*first)];
|
||||
char* buffer = new char[n * sizeof(*first)];
|
||||
// buffer size can be optimized to largest power of 2 less than n elements
|
||||
// divide the container into equally-sized segments whose length start at 1
|
||||
// and keeps increasing by factors of 2
|
||||
@@ -49,32 +49,28 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
* @param r points to the right part, end of left part
|
||||
* @param e points to end of right part
|
||||
* @param b points at the buffer
|
||||
*/
|
||||
template<class Iterator>
|
||||
*/
|
||||
template <class Iterator>
|
||||
void merge(Iterator l, Iterator r, const Iterator e, char b[]) {
|
||||
// create 2 pointers to point at the buffer
|
||||
auto p(reinterpret_cast<std::remove_reference_t<decltype(*l)>*>(b)), c(p);
|
||||
// move the left part of the segment
|
||||
for (Iterator t(l); r != t; ++t)
|
||||
*p++ = std::move(*t);
|
||||
for (Iterator t(l); r != t; ++t) *p++ = std::move(*t);
|
||||
// while neither the buffer nor the right part has been exhausted
|
||||
// move the smallest element of the two back to the container
|
||||
while (e != r && c != p)
|
||||
*l++ = std::move(*r < *c ? *r++ : *c++);
|
||||
while (e != r && c != p) *l++ = std::move(*r < *c ? *r++ : *c++);
|
||||
// notice only one of the two following loops will be executed
|
||||
// while the right part hasn't bee exhausted, move it back
|
||||
while (e != r)
|
||||
*l++ = std::move(*r++);
|
||||
while (e != r) *l++ = std::move(*r++);
|
||||
// while the buffer hasn't bee exhausted, move it back
|
||||
while (c != p)
|
||||
*l++ = std::move(*c++);
|
||||
while (c != p) *l++ = std::move(*c++);
|
||||
}
|
||||
/// bottom-up merge sort which sorts elements in a non-decreasing order
|
||||
/**
|
||||
* @param first points to the first element
|
||||
* @param n the number of elements
|
||||
*/
|
||||
template<class Iterator>
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const size_t n) {
|
||||
non_recursive_merge_sort(first, first + n, n);
|
||||
}
|
||||
@@ -82,8 +78,8 @@ void non_recursive_merge_sort(const Iterator first, const size_t n) {
|
||||
/**
|
||||
* @param first points to the first element
|
||||
* @param last points to 1-step past the last element
|
||||
*/
|
||||
template<class Iterator>
|
||||
*/
|
||||
template <class Iterator>
|
||||
void non_recursive_merge_sort(const Iterator first, const Iterator last) {
|
||||
non_recursive_merge_sort(first, last, last - first);
|
||||
}
|
||||
@@ -92,15 +88,15 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last) {
|
||||
* Currently, it only creates output for non_recursive_merge_sort.cpp, but if
|
||||
* it has proven its efficacy it can be expanded to other files.
|
||||
* The configuration file is named doxy.txt and has been auto-generated too.
|
||||
*/
|
||||
*/
|
||||
// the remaining of this file is only for testing. It can erased to to convert
|
||||
// it into a header file for later re-use.
|
||||
#include <iostream>
|
||||
int main(int argc, char ** argv) {
|
||||
int main(int argc, char** argv) {
|
||||
int size;
|
||||
std::cout << "Enter the number of elements : ";
|
||||
std::cin >> size;
|
||||
int * arr = new int[size];
|
||||
int* arr = new int[size];
|
||||
for (int i = 0; i < size; ++i) {
|
||||
std::cout << "arr[" << i << "] = ";
|
||||
std::cin >> arr[i];
|
||||
|
||||
Reference in New Issue
Block a user