0% found this document useful (0 votes)
15 views4 pages

#Include - Iostream

The document contains a C++ program that generates random numbers, saves them to a file, and implements both sequential and parallel merge sort algorithms. It utilizes multithreading to improve sorting performance based on the system's hardware concurrency. The program measures and outputs the time taken to sort the numbers for varying sizes, demonstrating the efficiency of the parallel sorting approach.

Uploaded by

sheoranpooja726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

#Include - Iostream

The document contains a C++ program that generates random numbers, saves them to a file, and implements both sequential and parallel merge sort algorithms. It utilizes multithreading to improve sorting performance based on the system's hardware concurrency. The program measures and outputs the time taken to sort the numbers for varying sizes, demonstrating the efficiency of the parallel sorting approach.

Uploaded by

sheoranpooja726
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <vector>
#include <thread>
#include <chrono>
#include <fstream>
#include <cstdlib>

using namespace std;


using namespace chrono;

int THREAD_THRESHOLD=std::thread::hardware_concurrency();

void generateNumbersToFile(const string& filename, int count) {


ofstream file(filename);
if (!file) {
cerr << "Error opening file for writing!" << endl;
return;
}

for (int i = 0; i < count; i++) {


file << rand() % 100 << "\n";
}
Type to enter text
file.close();
cout << "Random numbers saved to " << filename << endl;
}

vector<int> readNumbersFromFile(const string& filename) {


vector<int> numbers;
ifstream file(filename);
if (!file) {
cerr << "Error opening file for reading!" << endl;
return numbers;
}

int num;
while (file >> num) {
numbers.push_back(num);
}

file.close();
return numbers;
}

void merge(vector<int>& arr, int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

vector<int> L(n1), R(n2);

for (int i = 0; i < n1; i++) L[i] = arr[left + i];


for (int i = 0; i < n2; i++) R[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}

while (i < n1) arr[k++] = L[i++];


while (j < n2) arr[k++] = R[j++];
}

void mergeSortSequential(std::vector<int>& arr, int left, int right)


{
if (left < right) {
int mid = left + (right - left) / 2;

mergeSortSequential(arr, left, mid);


mergeSortSequential(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

void mergeSortParallel(vector<int>& arr, int left, int right, int


depth) {
if (left < right) {
int mid = left + (right - left) / 2;

if (depth > 0) {

std::thread leftThread([&arr, left, mid, depth]() {


mergeSortParallel(arr, left, mid, depth - 1);
});
leftThread.join();
} else {

mergeSortSequential(arr, left, mid);


mergeSortSequential(arr, mid + 1, right);
}

merge(arr, left, mid, right);


}
}

int main() {
}

string inputFile = "numbers.txt";


generateNumbersToFile(inputFile, 100000);

vector<int> arr = readNumbersFromFile(inputFile);

for(int i=10;i<=100000;i*=10){

auto start=chrono::high_resolution_clock::now();
mergeSortParallel(arr,0,i,THREAD_THRESHOLD);
auto stop=chrono::high_resolution_clock::now();
auto
interval=chrono::duration_cast<chrono::microseconds>(stop-
start);
int ans=interval.count();
cout<<ans<<endl;
}

return 0;
}

You might also like