-1

I searched for the implementation of suffix array in C, but all the programs I saw were in C++ which used sort. I am not sure how can I use the built-in function of C, qsort() in place of sort() function of C. Can we implement suffix arrays without using qsort()? or how to use qsort() to implement suffix arrays in C?

here is the code that I got from geeksforgeeks.com:

int cmp(struct suffix a, struct suffix b) 
{ 
return strcmp(a.suff, b.suff) < 0? 1 : 0; 
}

int *buildSuffixArray(char *txt, int n) 
{ 
// A structure to store suffixes and their indexes 
struct suffix suffixes[n]; 

// Store suffixes and their indexes in an array of structures. 
// The structure is needed to sort the suffixes alphabatically 
// and maintain their old indexes while sorting 
for (int i = 0; i < n; i++) 
{ 
    suffixes[i].index = i; 
    suffixes[i].suff = (txt+i); 
} 

// Sort the suffixes using the comparison function 
// defined above. 
sort(suffixes, suffixes+n, cmp); 

// Store indexes of all sorted suffixes in the suffix array 
int *suffixArr = new int[n]; 
for (int i = 0; i < n; i++) 
    suffixArr[i] = suffixes[i].index; 

// Return the suffix array 
return  suffixArr; 
} 

the cmp function is comparing structure data type while I am getting an error when using qsort(), which says that only void input is allowed.

  • What is stopping you from using `qsort`? – Scott Hunter Dec 04 '20 at 15:39
  • You use `qsort()` the same way you'd use it in any other context, what's different about suffix arrays? – Barmar Dec 04 '20 at 15:41
  • 1
    It sounds like you're just not sure how to use `qsort()` to sort an array of structs. – Barmar Dec 04 '20 at 15:44
  • So can you guide me on how to use qsort –  Dec 04 '20 at 15:45
  • 1
    Start by reading the [man page](https://linux.die.net/man/3/qsort). That describes how it works and has an example, so that should get you started. – dbush Dec 04 '20 at 15:47
  • `in C without using qsort?` So with or without using `qsort`? – KamilCuk Dec 04 '20 at 15:50
  • Actually, I am confused as to how to use the cmp function with structure data type as parameters. –  Dec 04 '20 at 15:51
  • In the `cmp()` function, first define two local variables as pointers to your `struct` type. Then cast each of the arguments to those pointers. Then make the comparisons and return -1, 0 or 1. With practice, you might not need the local pointers. – Weather Vane Dec 04 '20 at 15:54
  • Is your real question "how to use `qsort`"? – Eugene Sh. Dec 04 '20 at 15:54

1 Answers1

0

The declaration of the qsort function is as follows:

void qsort(void *base, size_t nmemb, size_t size,
       int (*compar)(const void *, const void *));

You'll notice that the comparison function it accepts must be defined to take a const void * for each of its two parameters, but you're instead passing in a struct suffix for each one.

You need to change your comparison function to use the parameter types that qsort expects. Then you can convert those parameters inside of the function to the proper pointer type and use those.

int cmp(const void *p1, const void *p2) 
{ 
    const struct suffix *a = p1;
    const struct suffix *b = p2;
    return strcmp(a->suff, b->suff) < 0? 1 : 0; 
}
dbush
  • 205,898
  • 23
  • 218
  • 273