Question

In: Computer Science

sort_employees(Employee emps[], int size) (40 pts) This function should sort the input array based on each...

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;​

   }​

}

Solutions

Expert Solution

/* 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 */


Related Solutions

C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT