Question: Hi, I need help with this program here. I have to implement a function to replace elements (referenced at the end of the description). I
Hi, I need help with this program here. I have to implement a function to replace elements (referenced at the end of the description). I also have to implement the function swap using the given parameters (line number may be off with respect to the description).
I just want to say thanks for the help and I will leave a comment and thumbs up!

Code:
// Selection sort with pass-by-reference. This program puts values into an
// array, sorts them into ascending order and prints the resulting array.
#include
#include
using namespace std;
void selectionSort( int * const, const int ); // prototype
void swap( int * const, int * const ); // prototype
int main()
{
const int arraySize = 10;
int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
cout
for ( int i = 0; i
cout
selectionSort( a, arraySize ); // sort the array
cout
for ( int j = 0; j
cout
cout
} // end main
// function to sort an array
void selectionSort( int * const array, const int size )
{
int smallest; // index of smallest element
// loop over size - 1 elements
for ( int i = 0; i
{
smallest = i; // first index of remaining array
// loop to find index of smallest element
for ( int index = i + 1; index
if ( array[ index ]
smallest = index;
//TODO: call function swap to place the smallest remaining element in the next spot in the array
} // end if
} // end function selectionSort
// swap values at memory locations to which
// element1Ptr and element2Ptr point
The first iteration of the algorithm selects the smallest element in the array and swaps it with the first element. The second iteration selects the second-smallest element (which is the smallest element of the remaining elements) and swaps it with the second element. The algorithm continues until the last iteration selects the second-largest element and swaps it with the second-to-last index, leaving the largest element in the last index. After the ith iteration, the smallest items of the array will be sorted into increasing order in the first elements of the array. As an example, consider the array 34 56 410 77 51 93 30 5 52 A program that implements the selection sort first determines the smallest value (4) in the array, which is contained in element 2. The program swaps the 4 witlh the value in element 0 (34), resulting in 4 56 34 10 77 51 93 30 552 The program then determines the smallest value of the remaining elements (all elements except 4), which is 5. contained in element 8. The program swaps the 5 with the 56 in element 1, resulting in 4 5 34 10 77 51 93 30 56 52 On the third iteration, the program determines the next smallest value, 10, and swaps it with the value in element 2 (34) 4 5 10 34 77 51 93 30 56 52 The process continues until the array is fully sorted. 4 5 10 30 34 51 52 56 7793 After the first iteration, the smallest element is in the first position. After the second iteration, the two smallest elements are in order in the first two positions After the third iteration, the three smallest elements are in order in the first three positions. On line 47 calls function swap (lines 53-58) to place the smallest remaining element in the next spot in the array (i.e., exchange the array elements array[] and array[smallest]) Starting from line 54, implement the function swap using the given parameters
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
