The code, in main, performs two separate sorts, and outputs the results twice: the output is identical, although achieved in different ways. There are various utility functions (e.g., to populate an array with Emp objects, to print the contents of an array). However, the code of interest occurs in three functions:
-- main: this function performs the sorts
-- compA: comparison function for one sort
-- compB: comparison function for the other sort

For ease of reference, the two sorts in main are labeled:
-- Sort A uses comparison function compA
-- Sort B uses comparison function compB

Sort B happens to be done first.
Which sort is better--and why.

/* empSort.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxName    (80)
#define BuffSize   (280)

typedef unsigned int bool;
enum {false, true};

typedef struct Emp {
unsigned char lname[MaxName + 1]; /* + 1 for '�' */
unsigned char fname[MaxName + 1];
unsigned char nickname[MaxName + 1];
unsigned int   id;
unsigned char dept;
bool          married;
} Emp;

void set_name(char* target, char* source) {
if (strlen(source) > MaxName) return;
strcpy(target, source);

int compA(const void* item1, const void* item2) {
const Emp* emp1 = (const Emp*) item1;
const Emp* emp2 = (const Emp*) item2;

unsigned char buff1[BuffSize + 1];
unsigned char buff2[BuffSize + 1];

strcpy(buff1, emp1->lname);
strcat(buff1, emp1->fname);
strcpy(buff2, emp2->lname);
strcat(buff2, emp2->fname);

return strcmp(buff1, buff2);

int compB(const void* item1, const void* item2) {
const Emp** emp1 = (const Emp**) item1;
const Emp** emp2 = (const Emp**) item2;

unsigned char buff1[BuffSize + 1];
unsigned char buff2[BuffSize + 1];

strcpy(buff1, (*emp1)->lname);
strcat(buff1, (*emp1)->fname);
strcpy(buff2, (*emp2)->lname);
strcat(buff2, (*emp2)->fname);

return strcmp(buff1, buff2);

int main() {

const int n = 6;
Emp emps[n];

printf("**** Before sorting:n");
dump_emps1(emps, n);

int i;
Emp* emps_2[n];
for (i = 0; i < n; i++) emps_2[i] = emps + i;

/* Sort B */
qsort(emps_2, n, sizeof(Emp*), compB);
printf("n**** After 1st sort:n");
dump_emps2(emps_2, n);

/* Sort A */
qsort(emps, n, sizeof(Emp), compA);
printf("n**** After 2nd sort:n");
dump_emps1(emps, n);

return 0;

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Main difference between Sort A and Sort B consists on what it is actually sorted. Sort A uses qsort to order an array of Emp items, while Sort B manipulates (to order) the pointers referring Emp items. Despite this difference, the comparison (in both cases) desires to realize 2 strings that are compared based on the regular lexicographic natural defined order (from dictionary). The two strings are “last” and “first” names of the employee....

50% discount

$20.00 $10.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats