ninja.stop
ninja.stop

Reputation: 430

Weighted non overlapping job scheduling using C

I had taken this example which is in C++ http://www.geeksforgeeks.org/weighted-job-scheduling-log-n-time/

This is the C implementation

#include <stdio.h>
#include <stdlib.h>

#define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

//#ifndef max
//    #define max(a,b) ((a) > (b) ? (a) : (b))
//#endif

struct rent
{
    int starttime, endtime, profit;
};

int sort_event(const void * st1, const void * st2)
{
    struct rent s1, s2;
    s1.endtime = *(int*)st1;
    s2.endtime = *(int*)st2;
    return (s1.endtime < s2.endtime);
}

int binarySearch(struct rent rents[], int index)
{
    // Initialize 'lo' and 'hi' for Binary Search
    int lo = 0, hi = index - 1;

    // Perform binary Search iteratively
    while (lo <= hi)
    {
        int mid = (lo + hi) / 2;
        if (rents[mid].endtime <= rents[index].starttime)
        {
            if (rents[mid + 1].endtime <= rents[index].starttime)
                lo = mid + 1;
            else
                return mid;
        }
        else
            hi = mid - 1;
    }

    return -1;
}

int findMaxProfit(struct rent arr[], int n)
{
    qsort(arr, sizeof(arr+n), sizeof(int), sort_event);

    int *table = (int *) malloc(sizeof(n));
    table[0] = arr[0].profit;

    // Fill entries in table[] using recursive property
    for (int i=1; i<n; i++)
    {
        // Find profit including the current job
        int inclProf = arr[i].profit;
        int l = binarySearch(arr, i);
        if (l != -1)
            inclProf += table[l];

        // Store maximum of including and excluding
        table[i] = max(inclProf, table[i-1]);
    }

    // Store result and free dynamic memory allocated for table[]
    int result = table[n-1];
    free(table);

    return result;
}

int main()
{
    struct rent arr1[] = {{3, 10, 20}, {1, 2, 50}, {6, 19, 100}, {2, 100, 200}};
    int n = sizeof(arr1)/sizeof(arr1[0]);
    printf("\nOptimal profit is %d\n", findMaxProfit(arr1, n));
    return 0;
}

The result expected is 250. After further investigation, I found that qsort() in C has different implementation wrt. sort() which is incsort. However I am not sure whether this is the sole reason or what. Can anyone please suggest where might be the faux pas.

Upvotes: 0

Views: 102

Answers (2)

Lundin
Lundin

Reputation: 213832

"Functors" in C and C++ work differently. qsort and bsearch expects a function that return a value lesser than, equal or greater than 0, if s1 is lesser than, equal or greater than s2.

In C++ a functor would only do one of these, for example return true if lesser otherwise false.

Change the code to return s1.endtime - s2.endtime;

In addition, your call to qsort is nonsense. As pointed out in another answer, you give the wrong parameters. sizeof(arr+n) should just be n. qsort wants the number of items, not the number of bytes. And you are using sizeof incorrectly - you can't even use it on a function parameter like arr is.

Upvotes: 1

4386427
4386427

Reputation: 44274

To me it seems that you call qsort incorrectly.

Try this instead:

qsort(arr,     n,            sizeof(struct rent), sort_event);
              ^^^                ^^^^
  Number of elements          Element size

Upvotes: 1

Related Questions