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.
.