Question: help check and correct my program #include #include #include #include #include #include using namespace std; mutex swapMutex; unsigned long long globalSwapCount = 0 ; /
help check and correct my program
#include
#include
#include
#include
#include
#include
using namespace std;
mutex swapMutex;
unsigned long long globalSwapCount ;
Bubble sort function for each thread
void bubbleSortvector& array, int start, int end, unsigned long long& threadSwapCount
for int i start; i end ; i
for int j start; j end i start; j
if arrayj arrayj
swaparrayj arrayj ;
threadSwapCount;
lockguard lockswapMutex;
globalSwapCount threadSwapCount;
Merge two sorted sections
void mergeSectionsvector& array, int start int end int start int end
vector tempend start;
int i start j start k ;
while i end && j end
if arrayi arrayj tempk arrayi;
else tempk arrayj;
while i end tempk arrayi;
while j end tempk arrayj;
for int p ; p k; p arraystart p tempp;
int main
Load numbers from file into array
vector array;
ifstream inFilenumbersdat";
for int i ; i && inFile arrayi; i;
inFile.close;
Initialize threads and swap count for each thread
vector threads;
unsigned long long threadSwapCounts;
for int i ; i ; i
int start i ;
int end i ;
threads.emplacebackbubbleSort refarray start, end, refthreadSwapCountsi;
Join all threads
for auto& th : threads thjoin;
Merge sorted sections iteratively
int sectionSize ;
while sectionSize
for int i ; i ; i sectionSize
int mid i sectionSize;
int end mini sectionSize;
if mid end mergeSectionsarray i mid, mid, end;
sectionSize ;
Output sorted array and global swap count
ofstream outFilemysortout";
for int i ; i ; i
outFile arrayi;
outFile.close;
cout "Total swaps: globalSwapCount endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
