1 answer

Given an integer array a[ ] of N elements. Please write an OpenMP function to sort...

Question:

Given an integer array a[ ] of N elements. Please write an OpenMP function to sort it by the Quicksort algorithm using the ta

Given an integer array a[ ] of N elements. Please write an OpenMP function to sort it by the Quicksort algorithm using the task directive. The function header is: void quicksort(int *a, int p, int r). (p represents the start index and r represents the end index)

Answers

NOTE:

HERE WE DID THE PROGRAM WITH C++ LANGUAGE

HERE TWO PROGRAMS ARE SHOWN IN C++

FIRST ONE IS DONE WITH OpenMP AND SECOND ONE WITH CONVENTIONAL PROCESS.

HERE WE TOOK LARGE AMOUNT OF DATA THROUGH RANDOM WAY.

AFTER QUICK SORT , HERE THE SORTED OUTPUTS ARE NOT SHOWN. BUT DISPLAY FUNCTIONS ARE WRITTEN IN BOTH THE CODE AND THEY ARE IN COMMENT SECTION.

ACTUALLY OpenMP FUNCTION IS USED TO REPRESENT PARALLEL PROGRAMMING. SO IT CAN SAVE THE EXECUTION TIME BY FASTER PROCESSING.

HERE THE PROCESSING TIMES FOR BOTH WITH AND WITHOUT OpenMP FUNCTION ARE CALCULATED AND SHOWN HOW OpenMP IS FASTER PROCESS TO SORT DATA.

/* PROGRAM WITH OpenMP FUNCTION */

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;


/* prototype of the function */
void quicksort(int *,int,int);
int partition(int *,int,int);
void sort_display(int *,int);

/* quicksort function defined */   
void quicksort(int *a,int p,int r)
{
   int u;

   if(p < r)
   { /* pivot value stored at j */
       u = partition(a,p,r);

       quicksort(a,p,u-1);
       quicksort(a,u+1,r);

   }
}
/* partition function defined to generate pivot value */
int partition(int *a, int p, int r)
{
   int i,j,t,k_val;
   k_val=a[p];
   i=p+1;
   j=r;
   while(1)
   {
       while(i<r && k_val>=a[i])
       {
           i++;
       }
       while(k_val<a[j])
       {
           j--;
       }      
       if(i<j)
       {
           t=a[i];
           a[i]= a[j];
           a[j]= t;
       }
       else
       {
           t=a[p];
           a[p]=a[j];
           a[j]=t;
           return j;
       }
   }
}
/* display in sorted form */
void sort_display(int *a,int n)
{
int i;
cout<<endl<<"Sorted array is:"<<endl;
for(i=0;i<n;i++)
{
cout<<" " <<a[i];
}
}
  

/* main function with OpenMP function */
int main()
{
   int i,j,start_time,stop_time,N;
   int a[500000];
   cout<<endl<<"How many elements:";
   cin>>N;
   /* start time of the program */
   start_time=clock();
   /* taking random values */
   for(i=0;i<N;i++)
   {
       a[i]=rand()%N;
   }
/* returning the pivot element of array */
   j = partition(a,0,N-1);
       #pragma openMP parallel sections // use of openMP
       {
           #pragma openMP section
           {
           /* openMP is used to quick sort left part of array with thread 1 */
                   quicksort(a,0, j - 1);
           }
           #pragma openMP section // use of openMP
           {
           /* openmp is used to quick sort right part of array with thread 2 */
                   quicksort(a, j + 1, N-1);
            }
       }
  
   /* finish time of the sorting */  
   stop_time=clock();
//   sort_display(a,N);
   /* total sorting time required with openMP */
   cout<<endl<<endl<<"Total Time taken with OpenMP:"<<(double)(stop_time - start_time)/CLOCKS_PER_SEC;
}

SCREEN SHOT

OUTPUT

/* PROGRAM WITHOUT OpenMP*/

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

/* prototype of the function */
void quicksort(int *,int,int);
int partition(int *,int,int);
void sort_display(int *,int);
void swap_value(int &,int &);

/* swap_value function defined to swap values for ascending order */
void swap_value(int* a,int* b)
{
int t;
   t=*a;
*a=*b;
*b=t;
}
/* partition function defined to generate pivot value */
int partition(int *a,int p,int r)
{
int pv_val,i,j;
pv_val=a[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(a[j]<pv_val)
{
i++;
           /* call by reference */
swap_value(&a[i], &a[j]);
}
}
swap_value(&a[i+1],&a[r]);
return(i+1);
}
  
/* quicksort function defined */
void quicksort(int *a, int p, int r)
{
   int u;

   if(p<r)
   { /* pv_val value stored at j */
       u=partition(a,p,r);

       quicksort(a,p,u-1);
       quicksort(a,u+1,r);

   }
}
  
/* display in sorted form */
void sort_display(int *a,int n)
{
int i;
cout<<endl<<"Sorted array is:"<<endl;
for(i=0;i<n;i++)
{
cout<<" " <<a[i];
}
}
  
/* main function without OpenMP function */
int main()
{
   int i,j,start_time,stop_time,N;
   int a[500000];
   cout<<endl<<"How many elements:";
   cin>>N;
   /* start time of the program */
   start_time=clock();
   /* taking random values */
   for(int i=0;i<N;i++)
   {
       a[i]=rand()%N;//filling random value
   }
quicksort(a, 0, N - 1);
stop_time=clock();
   //sort_display(a,N);
   cout<<endl<<endl<<"Total Time taken without OpenMP:"<<(double)(stop_time - start_time)/CLOCKS_PER_SEC;
}

SCREEN SHOT

OUTPUT

OUTPUT EXPLANATION:

HERE 450000 INTEGER VALUES ARE TAKEN FOR SORTING

IN FIRST CASE FOR OpenMP FUNCTION THREAD1 AND THREAD2 SORT LEFT AND RIGHT PORTION OF ARRAY IN PARALLEL.

SO IT TAKES LESS TIME 0.136 AS COMPARED TO NON OpenMP PROGRAM OF SORTING TIME 0.183

NOTE:

HERE NON OpenMP PROGRAM IS INTRODUCED ONLY TO GENERATE BETTER UNDERSTANDING OF OpenMP FUNCTION.

.

Similar Solved Questions

1 answer
1. Net present value (NPV) Evaluating cash flows with the NPV method The net present value...
1. Net present value (NPV) Evaluating cash flows with the NPV method The net present value (NPV) rule is considered one of the most common and preferred criteria that generally lead to good investment decisions. Consider this case: Suppose Fuzzy Button Clothing Company is evaluating a proposed capit...
1 answer
We believe we can sell 190,000 home security devices per year at $98 per piece. They...
We believe we can sell 190,000 home security devices per year at $98 per piece. They cost $87 to manufacture (variable cost). Fixed production costs run $215,000 per year. The necessary equipment costs $785,000 to buy and would be depreciated at a 25% CCA rate. The equipment would have a zero salvag...
1 answer
10.1 Utility Maximization Question Fday, Ap 313es PM Each week, Jen likes to drink coffee and...
10.1 Utility Maximization Question Fday, Ap 313es PM Each week, Jen likes to drink coffee and eat bagels in the morning either at work or at home on the weekends. Figure 1 lists the total utility that she receives at each quantity of cups of coffee and bagels, respectively. Additionally, Jen has a w...
1 answer
Chapter 10 Exercises i Saved SkyChefs, Inc., prepares in-flight meals for a number of major airlines....
Chapter 10 Exercises i Saved SkyChefs, Inc., prepares in-flight meals for a number of major airlines. One of the company's products is grilled salmon in dill sauce with baby new potatoes and spring vegetables. During the most recent week, the company prepared 7,000 of these meals using 2,700 dir...
1 answer
The owner of an apartment building can rent all 60 apartments if she charges $1,800 per...
The owner of an apartment building can rent all 60 apartments if she charges $1,800 per month, but she rents one fewer apartment for each $50 increase in monthly rent. (a) Construct a table that gives the revenue generated if she charges $1,800, $1,850, and $1,900. No. of Apts 60 Rent Total Revenue ...
1 answer
The manufacture of such products as penicillin, tetracycline, vitamins, and other pharmaceuticals, as well as photographic...
The manufacture of such products as penicillin, tetracycline, vitamins, and other pharmaceuticals, as well as photographic chemicals, dyes, and other fine organic compounds, usually requires separating the suspended solids from their mother liquid by centrifugation, and then drying the wet cake. A c...
1 answer
1) Read the title for each study and then identify if the study is: • experimental...
1) Read the title for each study and then identify if the study is: • experimental quasi-experimental, or descriptive. 1. Cardiometabolic Risk Factors Among 1.3 Million Adults with overweight or Obesity, but Not Diabetes, in 10 Geographically Diverse Regions of the United States, 2012 - 2013. 2...
1 answer
Q4(b) Two 555 timers are connected together as shown in Figure Q4(b). (i) Draw and completely...
Q4(b) Two 555 timers are connected together as shown in Figure Q4(b). (i) Draw and completely label the waveforms for 101, VC1, VO and VC3. All the waveforms must be drawn parallel at the time-axis and clearly show the relationship between them. (ii) Calculate the duty cycle of the output waveform V...
1 answer
Imagine you were to conduct an experiment where the manipulation had no effect whatsoever. If you...
Imagine you were to conduct an experiment where the manipulation had no effect whatsoever. If you repeated this experiment (that doesn't work) 100 times and computed the appropriate obtained statistic each time, approximately how many of the experiments out of 100 would you expect to find an eff...
1 answer
What are the components of the vector between the origin and the polar coordinate #(16, (7pi)/6)#?
What are the components of the vector between the origin and the polar coordinate #(16, (7pi)/6)#?...
1 answer
A recent study showed that 50 percent of households in Syracuse have at least 2 cars....
A recent study showed that 50 percent of households in Syracuse have at least 2 cars. Let X denote a binomially distributed random variable that is the number of households that have at least 2 cars. Out of 16 randomly chosen households, what is the probability that exactly nine have at least 2 ca...
1 answer
Your employer, a mid-sized human resources management company, is considering expansion into related fields, including the...
Your employer, a mid-sized human resources management company, is considering expansion into related fields, including the acquisition of Temp Force Company, an employment agency that supplies word processor operators and computer programmers to businesses with temporary heavy workloads. Your employ...
1 answer
PROBLEM:3 Sangria Boat Lifts purchased equipment on January 1, 2017 for $96,000. It is estimated that...
PROBLEM:3 Sangria Boat Lifts purchased equipment on January 1, 2017 for $96,000. It is estimated that the equipment will have a $5,000 residual value at the end of its 8-year useful life. It is also estimated that the equipment will produce 100,000 units over its 8- year life. Instructions: Answer t...
1 answer
With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()'...
With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()' functions in the inheriting class to work with multiple different polygons? I need them to work for polygon 'triangle' all the way to the polygon 'octagon', but a...
1 answer
The 400 V battery of a Tesla model S electric car stores 3.0 x 108J of...
The 400 V battery of a Tesla model S electric car stores 3.0 x 108J of energy. At 65 mph, the car can drive 250 mi before the battery is depleted. At this speed, what is the current delivered by the battery?...
1 answer
What is the solution: #6(n -2)>5n+ 5#?
What is the solution: #6(n -2)>5n+ 5#?...
1 answer
Look at this flower. Pretty crazy, eh? This is called fasciation, and may result from inappropriate...
Look at this flower. Pretty crazy, eh? This is called fasciation, and may result from inappropriate proliferation of the meristem during early development in the embryo. If this did indeed start early in development, from the list below, which is the most likely cause? An auxin maxima was establishe...
1 answer
P10-14 (similar to) : Question Help NPV. Huffman Systems has forecasted sales for its new home...
P10-14 (similar to) : Question Help NPV. Huffman Systems has forecasted sales for its new home alarm systems to be 60,000 units per year at $37.00 per unit. The cost to produce each unit is expected to be about 42% of the sales price. The new product will have an additional $490,000 of fixed costs e...
1 answer
Question 4 a) If a group G has order 323, what are the possible orders of...
Question 4 a) If a group G has order 323, what are the possible orders of its proper b) Let G<a > be a cyclic group of order 10. Is the map f: G-> G define«d subgroups: by Зі i. f(a) - a and f(a')-a agroup isomorphism? ii(a) and fa a a group isomorphism? 12, 5, 5...