Generic Quicksort - c

Below are the code snippets of generic qsort on C.
What do I write in the fourth parameter of the genmyqsort when it's called in the recursion?
int compnode(node *a, node *b){
return(strcmp(a->name,b->name));
}
void genmyqsort(void *a, int n, int size, int (*fcmp)(const void*,const void*)){
int pivot;
if(n>1){
pivot=partition(a,n,size);
genmyqsort(a*size, pivot,size);
genmyqsort(a+(pivot+1)*size,n-pivot-1,size);
}
}
call of Qsort in main.
genmyqsort(b,n,sizeof(node),(int(*)(const void*, const void*)) compnode);

You pass the same comparator as you got from the caller (fcmp):
genmyqsort(a*size, pivot, size, fcmp);
genmyqsort(a+(pivot+1)*size, n-pivot-1, size, fcmp);
This will ensure that all instances of genmyqsort() in the call tree will compare array elements in exactly the same way.

Related

callback function pointer with argument in C language

I have the following working code in C language, func1 and func2:
void func1(int (*callbackf)(void *, void *)){
void a = NULL;
void b=NULL;
//do some work and then call comp
callbackf (a,b)
}
void func2(int (*callbackf)(void *), void *a, void *b){
// do some work, not necessary works on a and b.
callbackf(a,b);
}
I am writing here to ask you if it is possible to do something like this (func3)
void func3(int (*callbackf)(void *), void *a, void *b){
// do some work, not necessary works on a and b.
callbackf(a,b);
}
is it possible? does it make sense?
regards
Alex

How to pass an array of function pointers as an argument?

I have an array of function pointers int (*oper_ptr[4])(int, int) = {add, sub, mul, divi}; for the below functions which simply perform a standard operation on two passed integers:
int add(int num_a, int num_b);
int sub(int num_a, int num_b);
int mul(int num_a, int num_b);
int divi(int num_a, int num_b);
What is the best way to pass this array into another function. I have tried:
void polish(char* filename, int (*oper_ptr[4])(int, int)) with for e.g., oper_ptr[0](num_a, num_b); to call the first function.
The way you have done it works. But as always with function pointers, it's a bad idea to use them without typedef:
typedef int operation (int num_a, int num_b);
void polish (char* filename, operation* op[4]);

Array of pointers to linked list

I need to know if i want to make an array that every element of the array is a pointer to a linked list and pass the array to a function, the function is void because I need to change the array
typedef struct n{
char *S;
int num;
}list;
int (main){
list *Array[50];
return 0;
}
should the function be void changeArray(list A[]); or void changeArray(list *A[]); or void changeArray(list **A[]);
The function could be either void changeArray(list *A[]) or void changeArray(list **A). Both signatures would accept an array of pointers, and let you change elements of that array:
void changeArray(list *A[]) {
...
A[0] = malloc(list);
}
The array is defined like
list *Array[50];
So if you want to pass this array to a function then it should be declared like
void changeArray( list *Array[50], size_t n );
or
void changeArray( list *Array[], size_t n );
or
void changeArray( list **Array, size_t n );
and called like
changeArray( Array, 50 );
In any case a declaration of an array as a function parameter is adjusted to a pointer to object of the type of elements of the array.

Trouble Sorting a 2d struct Array in C

I have a (150x150)2d array of structs that i'd like to sort in a row independent fashion. Because it is a struct, i do not believe (correct me if i'm wrong here) i cannot use qsort() or at least do not know how to due to the fact i'm paring structs and the element i am comparing is a double which goes against the compare prototype requirement of qsort(). At any rate i'd like to apply quicksort on
struct my_struct {
int x;
int y;
double d;
};
void quicksort(struct my_struct* array,int start, int end)
{
struct my_struct key, Pivot;
int i,j,PivotPoint;
if(start< end)
{
PivotPoint = (start+end)/2;
theswap(&array[start], &array[PivotPoint]);
key = array[start];
i= start+1;
j = end;
while (i<=j)
{
while((i<=end) && (array[i].d <= key.d))
++i;
while ((j>=start) && array[j].d> key.d) {
--j;
if (i<j) {
theswap(&array[i], &array[j]);
}
}
}
theswap(&array[start], &array[j]);
quicksort(array, start, j-1);
quicksort(array, j+1, end);
}
}
void theswap(struct my_struct *a, struct TourElement *b)
{
struct my_struct t;
t=*a;
*a=*b;
*b=t;
}
in my main function
i have something
like this:
for (i=0;i<150;++i)
{
for (j=0;j<150;++j)
{
My_array[i][j].x = somethingUseful;
My_Array[i][j].y = somethingEquallyUseful;
My_Array[i][j].d = CalcD(somethingUseful,somethingEquallyUseful);
}
qsort(My_Array[i],150,sizeof(my_struct),compare);
}
int compare(struct my_struct a , struct my_struct b)
{
return a.d -b.d;
}
When i execute quicksort, the application hangs, upon further investigation, there doesn't seem to be any elements in array within the quicksort function. (I added a for loop printf at the beginning of quicksort to itemize the struct's d values and nothing was printed)
Can anyone identify what i am doing wrong here? I'm getting no compile errors. And The "D"s are calculated correctly.
you can use std c qsort
void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );
the compare function is something like this:
int my_struct_comp(const void *p1, const void *p2){
my_struct *mp1 = (my_Struct*)p1;
my_struct *mp2 = (my_Struct*)p2;
return mp1->d - mp2->d;
}
than you can call qsort (where len is the length of the array)
qsort(myarray, len, sizeof(my_struct), &my_struct_cmp);

How do I pass in a multidimensional int array into a function in C?

As above, say I have a 3 dimensional array, a[][][], and I want to pass this to a function; how should I declare the function parameter?
void function1(int array[][3][4])
{
...use array here...
}
void function2(void)
{
int array[20][3][4];
...load array...
function1(array);
}
Just declare a triple pointer
int functionName(int*** arrayPtr, int x, int y, int z){
return arrayPtr[z][y][x];
}
I would send a pointer to pointer to pointer with all dimensions.
void foo(int ***ar, size_t l, size_t m, size_t n)
{ /* ... */ }

Resources