/*
Task 1:
This program sorts an array of EMPLOYEE structures using the insertion sort algorithm.
There are two sort functions:
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast); // sort ascending order by class
ID
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast); // sort ascending order by name
void insertionSortC(EMPLOYEE list[], EMPLOYEE *pLast); // sort descending order by year
REQUIREMENTS (Use C Language) :
1. Replace the three sorting functions with one generic sort:
void insertionSort(EMPLOYEE list[], EMPLOYEE *pLast, int compare(void *, void *));
2. Define a compare function to be used as a parameter for the first call of the generic sort:
int compareIds(void *p1, void *p2);
3. Define a compare function to be used as a parameter for the second call of the generic sort:
int compareNames(void *p1, void *p2);
4. Define a compare function to be used as a parameter for the third call of the generic sort:
int compareYears(void *p1, void *p2);
5. Replace the calls for the given three sort functions with calls for the generic sort function
6. Run the program.
7. Save the output as a comment at the end of the source file.
*~*/
#include
#include
#define NUM_EMP 500
typedef struct
{
char id[16];
char name[64];
int year;
} EMPLOYEE;
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast);
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast);
void insertionSortC(EMPLOYEE list[], EMPLOYEE *pLast);
void printList(EMPLOYEE list[], EMPLOYEE *pLast, char *description);
int main (void)
{
EMPLOYEE*pLast;
EMPLOYEE empList[NUM_EMP] =
{
{"9182", "John", 2011},
{"7364", "Ann", 2007},
{"1829", "Linda", 2022},
{"3647", "Jack", 1997},
{"8245", "Vince", 1995},
{"6473", "Jane", 1999},
{"8209", "Megan", 2003},
{"3637", "Jim", 2013},
{"6443", "Mary", 1998},
{"8809", "Tom", 2019},
{"3633", "Bob", 2021}
};
int numberOfEmployees = 11;
pLast = empList + numberOfEmployees - 1;
printList(empList, pLast, "Unsorted");
// first sort
insertionSortA(empList, pLast);
printList(empList, pLast, "Sorted by ID");
// second sort
insertionSortB(empList, pLast);
printList(empList, pLast, "Sorted by name");
// third sort
insertionSortC(empList, pLast);
printList(empList, pLast, "Sorted by year (descending)");
return 0;
}
/*~*~*~*~*~*~
Sort list using Insertion Sort.
Sort by ascending order of id
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged.
*~*/
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast)
{
EMPLOYEE temp;
EMPLOYEE *pCurr;
EMPLOYEE *pWalk;
for (pCurr = list + 1; pCurr <= pLast; pCurr++)
{
temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= list && strcmp(temp.id, pWalk->id) < 0)
{
*(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
}
/*~*~*~*~*~*~
Sort list using Insertion Sort.
Sort by ascending order of number of name.
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged
*~*/
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast)
{
EMPLO.
Measures of Central Tendency: Mean, Median and Mode
Task 1This program sorts an array of EMPLOYEE structures usin.pdf
1. /*
Task 1:
This program sorts an array of EMPLOYEE structures using the insertion sort algorithm.
There are two sort functions:
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast); // sort ascending order by class
ID
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast); // sort ascending order by name
void insertionSortC(EMPLOYEE list[], EMPLOYEE *pLast); // sort descending order by year
REQUIREMENTS (Use C Language) :
1. Replace the three sorting functions with one generic sort:
void insertionSort(EMPLOYEE list[], EMPLOYEE *pLast, int compare(void *, void *));
2. Define a compare function to be used as a parameter for the first call of the generic sort:
int compareIds(void *p1, void *p2);
3. Define a compare function to be used as a parameter for the second call of the generic sort:
int compareNames(void *p1, void *p2);
4. Define a compare function to be used as a parameter for the third call of the generic sort:
int compareYears(void *p1, void *p2);
5. Replace the calls for the given three sort functions with calls for the generic sort function
6. Run the program.
7. Save the output as a comment at the end of the source file.
*~*/
#include
#include
#define NUM_EMP 500
typedef struct
{
char id[16];
char name[64];
int year;
} EMPLOYEE;
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast);
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast);
void insertionSortC(EMPLOYEE list[], EMPLOYEE *pLast);
void printList(EMPLOYEE list[], EMPLOYEE *pLast, char *description);
int main (void)
2. {
EMPLOYEE*pLast;
EMPLOYEE empList[NUM_EMP] =
{
{"9182", "John", 2011},
{"7364", "Ann", 2007},
{"1829", "Linda", 2022},
{"3647", "Jack", 1997},
{"8245", "Vince", 1995},
{"6473", "Jane", 1999},
{"8209", "Megan", 2003},
{"3637", "Jim", 2013},
{"6443", "Mary", 1998},
{"8809", "Tom", 2019},
{"3633", "Bob", 2021}
};
int numberOfEmployees = 11;
pLast = empList + numberOfEmployees - 1;
printList(empList, pLast, "Unsorted");
// first sort
insertionSortA(empList, pLast);
printList(empList, pLast, "Sorted by ID");
// second sort
insertionSortB(empList, pLast);
printList(empList, pLast, "Sorted by name");
// third sort
insertionSortC(empList, pLast);
printList(empList, pLast, "Sorted by year (descending)");
return 0;
}
/*~*~*~*~*~*~
Sort list using Insertion Sort.
Sort by ascending order of id
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged.
3. *~*/
void insertionSortA(EMPLOYEE list[], EMPLOYEE *pLast)
{
EMPLOYEE temp;
EMPLOYEE *pCurr;
EMPLOYEE *pWalk;
for (pCurr = list + 1; pCurr <= pLast; pCurr++)
{
temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= list && strcmp(temp.id, pWalk->id) < 0)
{
*(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
}
/*~*~*~*~*~*~
Sort list using Insertion Sort.
Sort by ascending order of number of name.
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged
*~*/
void insertionSortB(EMPLOYEE list[], EMPLOYEE *pLast)
{
EMPLOYEE temp;
EMPLOYEE *pCurr;
EMPLOYEE *pWalk;
for (pCurr = list + 1; pCurr <= pLast; pCurr++)
{
temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= list && strcmp(temp.name, pWalk->name) < 0 )
{
4. *(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
}
/*~*~*~*~*~*~
Sort list using Insertion Sort.
Sort by descending order of year.
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged
*~*/
void insertionSortC(EMPLOYEE list[], EMPLOYEE *pLast)
{
EMPLOYEE temp;
EMPLOYEE *pCurr;
EMPLOYEE *pWalk;
for (pCurr = list + 1; pCurr <= pLast; pCurr++)
{
temp = *pCurr;
pWalk = pCurr - 1;
while (pWalk >= list && temp.year > pWalk->year )
{
*(pWalk + 1) = *pWalk;
pWalk--;
}
*(pWalk + 1) = temp;
}
}
/*~*~*~*~*~*~
Print list prints.
Pre list[] must contain at least one element
size is index to last element in list
Post list has been rearranged
*~*/