Comparing Quicksort Approaches: Emp Struct vs Emp* Array

  • Thread starter Thread starter ladesidude
  • Start date Start date
  • Tags Tags
    Comparison
Click For Summary
SUMMARY

The discussion compares two approaches to implementing the Quicksort algorithm using a structure called Emp in C. The first approach, comp1, directly compares Emp structures, while the second approach, comp2, uses pointers to pointers to Emp. It is concluded that both methods may be inefficient for large datasets, and the use of profiling tools is recommended to evaluate performance. Alternative comparison functions, such as compar and compar1, are suggested for potentially better performance.

PREREQUISITES
  • C programming language proficiency
  • Understanding of the qsort function
  • Familiarity with memory management in C
  • Knowledge of string manipulation functions like strcpy and strcmp
NEXT STEPS
  • Learn how to use profiling tools to analyze C code performance
  • Explore advanced sorting algorithms beyond Quicksort
  • Investigate memory management techniques in C for optimizing data structures
  • Study the impact of different comparison functions on sorting efficiency
USEFUL FOR

Software developers, particularly those working with C programming, data structure optimization, and performance analysis of sorting algorithms.

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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K