- #1
ladesidude
- 4
- 0
typedef struct Emp
{
unsigned char lname[MaxName + 1]; /* + 1 for '\0' */
unsigned char fname[MaxName + 1];
unsigned int id;
unsigned char dept;
bool married;
} Emp;
int comp1(const void* item1, const void* item2)
{
const Emp* emp1 = (const Emp*) item1; //emp1 points to Emp
const Emp* emp2 = (const Emp*) item2;
unsigned char buff1[BuffSize];
unsigned char buff2[BuffSize];
strcpy(buff1, emp1->lname);
strcat(buff1, emp1->fname);
strcpy(buff2, emp2->lname);
strcat(buff2, emp2->fname);
return strcmp(buff1, buff2);
}
int comp2(const void* item1, const void* item2)
{
const Emp** emp1 = (const Emp**) item1; // pointer to pointer to Emp
const Emp** emp2 = (const Emp**) item2;
unsigned char buff1[BuffSize];
unsigned char buff2[BuffSize];
strcpy(buff1, (*emp1)->lname);
strcat(buff1, (*emp1)->fname);
strcpy(buff2, (*emp2)->lname);
strcat(buff2, (*emp2)->fname);
return strcmp(buff1, buff2);
}
/* 1st qsort: better or worse approach than 2nd? why? emps here is an array*/
qsort(emps, n, sizeof(Emp), comp1);
/* 2nd qsort: better or worse approach than 1st? why?
Emp* emps_a[6] */
qsort(emps_a, n, sizeof(Emp*), comp2);
Based on the above code, which is the better quicksort and why?
thanks, this really has me in a bind...:(
{
unsigned char lname[MaxName + 1]; /* + 1 for '\0' */
unsigned char fname[MaxName + 1];
unsigned int id;
unsigned char dept;
bool married;
} Emp;
int comp1(const void* item1, const void* item2)
{
const Emp* emp1 = (const Emp*) item1; //emp1 points to Emp
const Emp* emp2 = (const Emp*) item2;
unsigned char buff1[BuffSize];
unsigned char buff2[BuffSize];
strcpy(buff1, emp1->lname);
strcat(buff1, emp1->fname);
strcpy(buff2, emp2->lname);
strcat(buff2, emp2->fname);
return strcmp(buff1, buff2);
}
int comp2(const void* item1, const void* item2)
{
const Emp** emp1 = (const Emp**) item1; // pointer to pointer to Emp
const Emp** emp2 = (const Emp**) item2;
unsigned char buff1[BuffSize];
unsigned char buff2[BuffSize];
strcpy(buff1, (*emp1)->lname);
strcat(buff1, (*emp1)->fname);
strcpy(buff2, (*emp2)->lname);
strcat(buff2, (*emp2)->fname);
return strcmp(buff1, buff2);
}
/* 1st qsort: better or worse approach than 2nd? why? emps here is an array*/
qsort(emps, n, sizeof(Emp), comp1);
/* 2nd qsort: better or worse approach than 1st? why?
Emp* emps_a[6] */
qsort(emps_a, n, sizeof(Emp*), comp2);
Based on the above code, which is the better quicksort and why?
thanks, this really has me in a bind...:(