Write a program to implement job sequencing algorithm using greedy
approach.
#include<stdio.h>
#include<conio.h>
int jobsequence(int d[6],int j[6],int n)
{
int q,i,r,k;
d[0]=0;
j[0]=0;
j[1]=1;
k=1;
for(i=2;i<=n;i++)
{
r=k;
while((d[j[r]]>d[i]) &&(d[j[r]]!=r))
r=r-1;
if((d[j[r]]<=d[i]) && (d[i]>r))
{
for(q=k;q>=r+1;q--)
{
j[q+1]=j[q];
}
j[r+1]=i;
k=k+1;
}
}
return k;
}
void main( )
{
int d[6],j[6],p[6],k,i;
clrscr( );
printf("Enter the deadlines :");
for(i=1;i<=5;i++)
scanf("%d",&d[i]);
printf("Enter the profits :");
for(i=1;i<=5;i++)
scanf("%d",&p[i]);
for(i=1;i<=5;i++)
j[i]=i;
k=jobsequence(d,j,5);
printf("\nThe solution job sequence is ");
for(i=1;i<=k;i++)
printf("\n%d",j[i]);
getche( );
}
OUTPUT
Enter the deadlines :2
2
1
3
3
Enter the profits :18
14
12
5
1
The solution job sequence is
1
2
4
Complexity :-