#include<iostream>
using namespace std;
struct job
{
char c;
int p;
int d;
};
int partition(job a[],int p,int r)
{
int x;
x=a[r].p;
int i=p-1;
for(int j=p; j<=r-1; j++)
{
if(a[j].p<=x)
{
i=i+1;
int t=a[i].p;
a[i].p=a[j].p;
a[j].p=t;
}
int t=a[i+1].p;
a[i+1].p=a[r].p;
a[r].p=t;
}
cout<<endl<<"i+1 = "<<i+1;
return i+1;
}
void quicksort(job a[],int p,int r)
{
for(int i=0; i<=r; i++)
cout<<a[i].p<<" <--->"<<endl;
int q=0;
if(p<r)
{
q=partition(a,p,r);
cout<<"*******************************";
quicksort(a,p,q-1);
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*";
quicksort(a,q+1,r);
cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}
}
int main()
{
int n;
int x;
cout<<"Enter the no. of jobs : ";
cin>>n;
job j[n];
//int di[n]={0};
//cout<<"Enter the profits in decreasing order : \n";
cout<<"Enter job name : ";
for(int i=0; i<n; i++)
{
cin>>j[i].c;
}
cout<<"Enter profit : ";
for(int i=0; i<n; i++)
{
cin>>j[i].p;
}
cout<<"Enter Deadline : ";
for(int i=0; i<n; i++)
{
cin>>j[i].d;
}
cout<<"You Entered \n";
for(int i=0; i<n; i++)
{
cout<<j[i].c<<" ";
cout<<j[i].p<<" ";
cout<<j[i].d<<" ";
cout<<endl;
}
quicksort(j,0,n-1);
cout<<"Sorted profit is : "<<endl;
for(int i=0; i<n; i++)
{
cout<<j[i].p;
}
cout<<endl<<"You Entered \n";
for(int i=0; i<n; i++)
{
cout<<j[i].c<<" ";
cout<<j[i].p<<" ";
cout<<j[i].d<<" ";
cout<<endl;
}
}
Tuesday, 6 December 2016
quick_sort.cpp
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment