Skip to content

Commit

Permalink
Merge pull request #542 from aditmehta9/patch-1
Browse files Browse the repository at this point in the history
Job sequencing code in C++
  • Loading branch information
ambujraj authored Oct 7, 2018
2 parents 7f65e36 + ba9871f commit 8cf2839
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions job_sequencing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include<iostream>
#include<algorithm>
using namespace std;

struct Job
{
char id;
int dead;
int profit;
};

bool comparison(Job a, Job b)
{
return (a.profit > b.profit);
}
void printJobScheduling(Job arr[], int n)
{
sort(arr, arr+n, comparison);

int result[n];
bool slot[n];
for (int i=0; i<n; i++)
slot[i] = false;

for (int i=0; i<n; i++)
{
for (int j=min(n, arr[i].dead)-1; j>=0; j--)
{

if (slot[j]==false)
{
result[j] = i;
slot[j] = true;
break;
}
}
}

for (int i=0; i<n; i++)
if (slot[i])
cout << arr[result[i]].id << " ";
}

int main()
{
Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
{'d', 1, 25}, {'e', 3, 15}};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Following is maximum profit sequence of jobsn";
printJobScheduling(arr, n);
return 0;
}

0 comments on commit 8cf2839

Please sign in to comment.