TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
non_recursive_merge_sort.cpp File Reference
#include <cstddef>
#include <iostream>
#include <utility>
Include dependency graph for non_recursive_merge_sort.cpp:

Go to the source code of this file.

Namespaces

namespace  sorting
 for working with vectors
 

Functions

template<class Iterator>
void sorting::merge (Iterator l, Iterator r, const Iterator e, char b[])
 merges 2 sorted adjacent segments into a larger sorted segment
 
template<class Iterator>
void sorting::non_recursive_merge_sort (const Iterator first, const Iterator last, const size_t n)
 bottom-up merge sort which sorts elements in a non-decreasing order
 
template<class Iterator>
void sorting::non_recursive_merge_sort (const Iterator first, const size_t n)
 bottom-up merge sort which sorts elements in a non-decreasing order
 
template<class Iterator>
void sorting::non_recursive_merge_sort (const Iterator first, const Iterator last)
 bottom-up merge sort which sorts elements in a non-decreasing order
 
int main (int argc, char **argv)
 
template<class Iterator>
void non_recursive_merge_sort (const Iterator first, const Iterator last, const size_t n)
 bottom-up merge sort which sorts elements in a non-decreasing order
 

Detailed Description

Copyright 2020

Author
Albirair

A generic implementation of non-recursive merge sort.

Definition in file non_recursive_merge_sort.cpp.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 94 of file non_recursive_merge_sort.cpp.

94 {
95 int size;
96 std::cout << "Enter the number of elements : ";
97 std::cin >> size;
98 int* arr = new int[size];
99 for (int i = 0; i < size; ++i) {
100 std::cout << "arr[" << i << "] = ";
101 std::cin >> arr[i];
102 }
103 non_recursive_merge_sort(arr, size);
104 std::cout << "Sorted array\n";
105 for (int i = 0; i < size; ++i)
106 std::cout << "arr[" << i << "] = " << arr[i] << '\n';
107 delete[] arr;
108 return 0;
109}
void non_recursive_merge_sort(const Iterator first, const Iterator last, const size_t n)
bottom-up merge sort which sorts elements in a non-decreasing order

◆ non_recursive_merge_sort()

template<class Iterator>
void sorting::non_recursive_merge_sort ( const Iterator first,
const Iterator last,
const size_t n )

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. best-case = worst-case = O(n log(n))

Parameters
firstpoints to the first element
lastpoints to 1-step past the last element
nthe number of elements

Definition at line 25 of file non_recursive_merge_sort.cpp.

26 {
27 // create a buffer large enough to store all elements
28 // dynamically allocated to comply with cpplint
29 char* buffer = new char[n * sizeof(*first)];
30 // buffer size can be optimized to largest power of 2 less than n
31 // elements divide the container into equally-sized segments whose
32 // length start at 1 and keeps increasing by factors of 2
33 for (size_t length(1); length < n; length <<= 1) {
34 // merge adjacent segments whose number is n / (length * 2)
35 Iterator left(first);
36 for (size_t counter(n / (length << 1)); counter; --counter) {
37 Iterator right(left + length), end(right + length);
38 merge(left, right, end, buffer);
39 left = end;
40 }
41 // if the number of remaining elements (n * 2 % length) is longer
42 // than a segment, merge the remaining elements
43 if ((n & ((length << 1) - 1)) > length)
44 merge(left, left + length, last, buffer);
45 }
46 delete[] buffer;
47}
void merge(int *arr, int l, int m, int r)