Comparing Quicksort Approaches: Emp Struct vs Emp* Array

  • Thread starter ladesidude
  • Start date
  • Tags
    Comparison
In summary, the code shows two different comparison functions for a quicksort algorithm. The first function, comp1, takes in two pointers to Emp structures and sorts them based on the concatenation of their last and first names. The second function, comp2, takes in two pointers to pointers to Emp structures and does the same comparison. In terms of efficiency, the second function may be better since it uses pointers to pointers instead of just pointers, but this would need to be tested with a profiler on a larger set of data. Additionally, a third comparison function, compar1, is suggested as a possible alternative to improve efficiency. Overall, the best quicksort algorithm would need to be determined through testing and optimization.
  • #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...:(
 
Technology news on Phys.org
  • #2
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
 
  • #3


I would say that both approaches have their own advantages and disadvantages, and it ultimately depends on the specific needs and requirements of the application.

The first approach (comp1) is using a struct to store and access the data, which can be more efficient in terms of memory usage and access time. However, it requires more steps to access and manipulate the data, as seen in the use of strcpy, strcat, and strcmp functions. This can result in slower performance for large datasets.

On the other hand, the second approach (comp2) is using a pointer to a pointer to access the data, which may be less efficient in terms of memory usage and access time. However, it allows for faster data manipulation as it directly accesses the data through pointers. This can be beneficial for large datasets and may result in better performance.

Therefore, it is difficult to say which approach is better without knowing more about the specific requirements and limitations of the application. It may be helpful to conduct further testing and analysis to determine which approach would be more suitable in a given scenario.
 

1. What is the purpose of comparing quicksort approaches?

The purpose of comparing quicksort approaches is to determine which approach is more efficient in terms of time and space complexity. This can help in choosing the most suitable approach for a specific problem or dataset.

2. What is the difference between Emp Struct and Emp* Array?

Emp Struct and Emp* Array are both implementations of the quicksort algorithm. The main difference is that Emp Struct uses a structure to store the elements to be sorted, while Emp* Array uses a pointer to an array of elements. This results in different memory usage and access times for the two approaches.

3. How do the two approaches perform in terms of time complexity?

The time complexity of both Emp Struct and Emp* Array is O(nlogn) in average case. However, Emp Struct has a slightly better performance due to its use of a structure, which allows for better cache utilization.

4. Which approach is more suitable for large datasets?

Emp* Array is more suitable for large datasets as it has a better space complexity compared to Emp Struct. This means that it can handle larger datasets without causing memory issues.

5. Is one approach always better than the other?

No, there is no one approach that is always better than the other. The performance of the two approaches may vary depending on the dataset and the hardware being used. It is important to test and compare the two approaches in different scenarios to determine which one is more suitable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top