79581492

Date: 2025-04-18 17:46:46
Score: 1.5
Natty:
Report link

I have been able to reach a solution through the utilization of OpenMP (thanks to @JérômeRichard for suggesting this tool to me).

#include <iostream>
#include <iomanip>
#include <ios>
#include <vector>
#include <random>
#include <omp.h>


void quicksort(std::vector<double> & v, int const begin, int const end){
    
    /*
    Quicksort algorithm omitted for brevity
    (As with every implementation, element swaps
    are made, and the necessary index i is
    created and initialized correctly)
    */

        #pragma omp task shared(v,begin,i)
        {
    quicksort(v, begin, i-1);
    }
        
        #pragma omp task shared(v,i,end)
        {
    quicksort(v,i+1,end);
    }
      
    return;
};


int main() {
    std::vector<double> vect1; // Initialization of vect1 to several million random elements omitted

    #pragma omp parallel
    {
      #pragma omp single
      { quicksort(vect1,0,vect1.size()-1); } 
    }

    // Omitted here: a for-loop to print some of the elements of vect1 to ensure the sort was successful

    return 0;
}

You will notice that the following expressions from OpenMP are used:

#pragma omp parallel
#pragma omp single
#pragma omp task shared(v,begin,i)
#pragma omp task shared(v,i,end)

And the header <omp.h> is included.

This is compiled with g++, and it is necessary to add the flag -fopenmp to allow for the use of OpenMP tools in the code:

g++ -W -Wall -pedantic -fopenmp -o ProgramName -p FileName.cpp
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @JérômeRichard
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: tiredUser