#include<iostream>
#include<omp.h>
using namespace std;
int thread_id,pass_count=0;
class sort
{
public:
int i,length,*array=NULL;
void input();
void display();
void quick_sort (int[],int,int);
int partition(int[],int,int);
};
void sort::input()
{
cout<<"Enter number of elements in the array"<<endl;
cin>>length;
array=new int [length];
cout<<"Enter the elements"<<endl;
for(i=0;i<length;i++)
{
cin>>array[i];
}
}
void sort::display()
{
for(i=0;i<length;++i)
{
cout<<array[i]<<"\t";
}
cout<<"\n";
}
void sort::quick_sort(int array[],int left_index,int right_index)
{
int j;
if(left_index<right_index)
{
j=partition(array,left_index,right_index);
++pass_count;
cout<<"Pass:" <<pass_count<<"\t";
display();
#pragma omp parallel sections
{
#pragma omp section
{
thread_id=omp_get_thread_num();
cout<<"\nLeft side array [";
for(int t=left_index;t<=j-1;t++)
cout<<array[t]<<"\t";
cout<<"] processed by Thread "<<thread_id<<endl;
quick_sort(array,left_index ,j-1);
}
#pragma omp section
{
thread_id=omp_get_thread_num();
cout<<"\nRight side array [";
for(int t=j+1;t<=right_index;t++)
cout<<array[t]<<"\t";
cout<<"] processed by Thread "<<thread_id<<"\n\n\n";
quick_sort(array,j+1,right_index);
}
}
}
}
int sort::partition(int array[],int left_index,int right_index)
{
int pivot,i,j,temp;
pivot=array[left_index];
i=left_index;
j=right_index+1;
while(1)
{
do
++i;
while(array[i]<pivot && i<=right_index);
do
--j;
while(array[j]>pivot);
if(i>=j)
break;
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
temp=array[left_index];
array[left_index]=array[j];
array[j]=temp;
return j;
}
int main()
{
sort obj;
obj.input();
cout<<"\nUnsorted array is:";
obj.display();
cout<<"\n\n";
obj.quick_sort(obj.array,0,(obj.length-1));
cout<<"\nSorted array is:";
obj.display();
}
OUTPUT:
No comments:
Post a Comment