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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top