In: Computer Science
sort_employees(Employee emps[], int size) (40 pts)
This function should sort the input array based on each employee’s
years_of_service. You are required to adapt the Selection Sort code
presented in Chapter 6 for the implementation of this function. You
can find the original code of the Selection Sort on slide 51 of the
chapter.
This is the Selection Sort talked about on the slide of 51. I am not sure where to even begin with this question.
for (int unsorted = 0; unsorted < size - 1; unsorted++)
{
// Find the position of the minimum
int min_pos = unsorted;
for (int i = unsorted + 1; i < size; i++)
{
if (values[i] < values[min_pos]) { min_pos = i; }
}
// Swap the minimum into the sorted area
if (min_pos != unsorted)
{
double temp = values[min_pos];
values[min_pos] = values[unsorted];
values[unsorted] = temp;
}
}
/* READ COMMENTS TO KNOW ? IS LINE DOING COPY PASTE CODE
TO SORT BASED ON years_of_service */
void sort_employees(Employee emps[], int size){
// One by one move boundary of unsorted subarray
for (int unsorted = 0; unsorted < size - 1;
unsorted++)
{
// Find the position of the minimum
int min_pos = unsorted;
for (int i = unsorted + 1; i < size; i++)
{
// compare years of
service
if (emps[i].years_of_service <
emps[min_pos].years_of_service)
{
min_pos = i;
}
}
// Swap the minimum into the sorted area
if (min_pos != unsorted)
{
// Swap the found minimum element
with the first element
Employee temp = emps[min_pos];
emps[min_pos] = emps[unsorted];
emps[unsorted] = temp;
}
}
}
/*

*/
/* PLEASE UPVOTE */