Comparing Quicksort Approaches: Emp Struct vs Emp* Array

  • Thread starter Thread starter ladesidude
  • Start date Start date
  • Tags Tags
    Comparison
AI Thread Summary
The discussion centers around two different implementations of comparison functions used with the C standard library's `qsort` function for sorting an array of employee structures (`Emp`). The first comparison function, `comp1`, directly compares the concatenated last and first names of `Emp` structures, while the second function, `comp2`, works with pointers to pointers of `Emp`, which adds an extra level of indirection. Participants debate which approach is better, with considerations for efficiency and clarity. The consensus suggests that both methods may be slower than simpler alternatives, and it's recommended to use profiling tools to evaluate performance with larger datasets. Additionally, alternative comparison functions are proposed, including one that uses `strcmp` for a primary comparison and falls back to comparing first names if last names are equal. Another suggestion involves using `memcmp` for direct memory comparison, contingent on proper initialization of the string buffers. Overall, the discussion emphasizes the importance of optimizing comparison algorithms for better performance in sorting operations.
ladesidude
Messages
4
Reaction score
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...:(
 
Technology news on Phys.org
how about
Code:
int compar(const void *A, const void *B)
{
     const char *a=(const char *)A;
     const char *b=(const char *)B;
     int retval=strcmp(a,b);
     if(!retval)
     {
           Emp *a1=(Emp *)A;
           Emp *b1=(Emp *)B;
           retval=strcmp(a1->fname, b1->fname);
      }
     return retval;
}

or just
Code:
int compar1(const void *A, const void *B)
{
     return memcmp(A, B, MaxName + 2);
}
The last one will work ONLY if you don't use strcpy to place values into those strings, and they are initialized to \0 for the full length using memset before they get a new value.

To answer your question - use a profiler on your code - test your qsort against say 20K records and either time the process or use a profiler. But the answer to your implied question is that both are probably slower than something less involved. Change your compare algorithms. Those above are possible starting points
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top