mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
added sorting algos to namespace sorting
This commit is contained in:
parent
c2c4681554
commit
5a2615c54e
@ -8,13 +8,14 @@
|
||||
#include <iostream>
|
||||
#include <utility> // for std::move & std::remove_reference_t
|
||||
|
||||
namespace sorting {
|
||||
template <class Iterator>
|
||||
void merge(Iterator, Iterator, const Iterator, char[]);
|
||||
/// bottom-up merge sort which sorts elements in a non-decreasing order
|
||||
/**
|
||||
* sorts elements non-recursively by breaking them into small segments, merging
|
||||
* adjacent segments into larger sorted segments, then increasing the sizes of
|
||||
* segments by factors of 2 and repeating the same process.
|
||||
* sorts elements non-recursively by breaking them into small segments,
|
||||
* merging adjacent segments into larger sorted segments, then increasing
|
||||
* the sizes of segments by factors of 2 and repeating the same process.
|
||||
* best-case = worst-case = O(n log(n))
|
||||
* @param first points to the first element
|
||||
* @param last points to 1-step past the last element
|
||||
@ -26,9 +27,9 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
// create a buffer large enough to store all elements
|
||||
// dynamically allocated to comply with cpplint
|
||||
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
|
||||
// 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
|
||||
for (size_t length(1); length < n; length <<= 1) {
|
||||
// merge adjacent segments whose number is n / (length * 2)
|
||||
Iterator left(first);
|
||||
@ -55,7 +56,8 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last,
|
||||
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);
|
||||
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);
|
||||
// while neither the buffer nor the right part has been exhausted
|
||||
@ -86,6 +88,10 @@ void non_recursive_merge_sort(const Iterator first, const Iterator last) {
|
||||
non_recursive_merge_sort(first, last, last - first);
|
||||
}
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
using sorting::non_recursive_merge_sort;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int size;
|
||||
std::cout << "Enter the number of elements : ";
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
namespace sorting {
|
||||
/**
|
||||
* This function takes last element as pivot, places
|
||||
* the pivot element at its correct position in sorted
|
||||
@ -67,6 +68,10 @@ void quickSort(int arr[], int low, int high) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sorting
|
||||
|
||||
using sorting::quickSort;
|
||||
|
||||
// prints the array after sorting
|
||||
void show(int arr[], int size) {
|
||||
for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
|
||||
|
Loading…
Reference in New Issue
Block a user