I tried to implement MergeSort parallel in C ++, which also tracks how many comparisons are made and how many threads it uses:

`#include `
#include
#include
#include
#include
#include
int *original_array,*auxiliary_array;
std::mutex protector_of_the_global_counter;
int global_counter=0;
std::mutex protector_of_the_thread_counter;
int number_of_threads=0;
template
class Counting_Comparator {
private:
bool was_allocated;
int *local_counter;
public:
Counting_Comparator() {
was_allocated=true;
local_counter=new int(0);
}
Counting_Comparator(int *init) {
was_allocated=false;
local_counter=init;
}
int get_count() {return *local_counter;}
bool operator() (T first, T second) {
(*local_counter)++;
return first &x) {
was_allocated=x.was_allocated;
local_counter=x.local_counter;
}
~Counting_Comparator() {
if (was_allocated) delete local_counter;
}
};
struct limits {
int lower_limit,upper_limit,reccursion_depth;
};
void parallel_merge_sort(limits argument) {
int lower_limit=argument.lower_limit;
int upper_limit=argument.upper_limit;
if (upper_limit-lower_limit<2) return; //An array of length less than 2 is already sorted.
int reccursion_depth=argument.reccursion_depth;
int middle_of_the_array=(upper_limit+lower_limit)/2;
limits left_part={lower_limit,middle_of_the_array,reccursion_depth+1},
right_part={middle_of_the_array,upper_limit,reccursion_depth+1};
if (reccursion_depth comparator_functor(&local_counter);
std::merge(original_array+lower_limit,
original_array+middle_of_the_array,
original_array+middle_of_the_array,
original_array+upper_limit,
auxiliary_array+lower_limit,
comparator_functor);
protector_of_the_global_counter.lock();
global_counter+=comparator_functor.get_count();
protector_of_the_global_counter.unlock();
std::copy(auxiliary_array+lower_limit,
auxiliary_array+upper_limit,
original_array+lower_limit);
}
int main(void) {
using std::cout;
using std::cin;
using std::endl;
cout <<"Enter how many numbers you will input." <>n;
try {
original_array=new int(n);
auxiliary_array=new int(n);
}
catch (...) {
std::cerr <<"Not enough memory!?" <>original_array(i);
limits entire_array={0,n,0};
number_of_threads=1;
clock_t processor_time=clock();
try {
std::thread root_of_the_reccursion(parallel_merge_sort,entire_array);
root_of_the_reccursion.join();
}
catch (std::system_error error) {
std::cerr <<"Can't create a new thread, error "" <

```
```So what do you think about it?

```
```