job sequencing program using greedy

 #include <iostream>

#define N 5
using namespace std;
void profitsort(int profit[], int w[], int n)
{
    int temp, i, j;
    for (i = 0; i < n - 1; i++)
    {
        for (j = i + 1; j < n; j++)

            if (profit[i] < profit[j])
            {

                temp = profit[i];
                profit[i] = profit[j];
                profit[j] = temp;

                temp = w[i];
                w[i] = w[j];
                w[j] = temp;
            }
    }
}
    int main()
    {
        int p[N], d[N], dmax;
        int profit = 0, i, j;
        cout << "Enter the profits for all jobs: ";
        for (i = 0; i < N; i++)
        {
            cin >> p[i];
        }
        cout << "Enter the deadlines for all the jobs: ";
        for (i = 0; i < N; i++)
        {
            cin >> d[i];
        }

        dmax = d[0];
        for (i = 1; i < N; i++)
        {

            if (d[i] > dmax)
            {
                dmax = d[i];
            }
        }
        profitsort(p,d,N);
        int s[dmax];
        for (i = 0; i < dmax; i++)
        {
            s[i] = -1;
        }
        for (i = 0; i < N; i++)
        {
            j = d[i]-1;
            while (j >= 0)
            {
                if (s[j] == -1)
                {
                    s[j] = i;
                    profit = profit + p[i];
                    break;
                }
                --j;
            }
        }
        for (i = 0; i < dmax; i++)
        {
            cout << s[i] << " ";
        }
        cout << endl<< profit;

        return 0;
    }

Comments

Popular Posts