Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise #1: Selection Sort Write a function called small) that takes three argu

ID: 3914225 • Letter: E

Question

Exercise #1: Selection Sort Write a function called small) that takes three arguments: the location of the first element k, the dimension nDim of a vector v, and the vector v itself, the function returns the index of the smallest element in the given vector from the location k to location nDim. Write a function called swap()that takes two arguments and swap their values Write a main() program that input the dimension nDim and the elements of the vector, sort the elements of the vector in ascending order, and output the sorted elements. One way to sort the elements of a vector (there are more efficient ways) is to do the following: Step 1: use the function small) to find the smallest element (among: x, xo,..,x), and use the function swop to exchange it with the first element of the vector. Step 2: use the function small) to find the smallest element (among: x xx), and use the function swap to exchange it with the second element of the vector. Step 3: use the function small() to find the smallest element (among: x, x , Kol, and use the function swop0 to exchange it with the third element of the vector Step n-1: use the function small) to find the smallest element (among: xn1, n), and use the function swap) to exchange it with the (n-1)th element of the vector. Example Stepi Step 2 Step 3 Step 4 Step 5 begin end begin end begin end begin end begin end Begin end 0 0 0 19 0 0 5 120 120 120 120| 120 120| 120 20| 120 | 120| 120 30 30 30 120 Sample input/ output: nter the size of the array: 10 nter the 16 elements of the array: 7 3 49711 15 1e 7 sorted elements of the array are: 4 7778910 11 15

Explanation / Answer

#include<iostream>

using namespace std;

//method to swap elements

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

//method to find in min element index from i to n in arrayay

int small(int i,int n,int array[])

{

int min_index,j;

min_index = i;

for (j = i+1; j < n; j++)

if (array[j] < array[min_index])

min_index = j;

return min_index;

}

//method to sort arrayay

void selectionSort(int array[], int n)

{

int i, j, min_index;

for (i = 0; i < n-1; i++)

{

min_index=small(i,n,array);

swap(&array[min_index], &array[i]);

}

}

//method to print arrayay

void print(int array[], int size)

{

int i;

for (i=0; i < size; i++)

cout<<array[i]<<" ";

cout<<" ";

}

//test method

int main()

{

//reading input

int n;

cout<<"Enter size of the arrayay:";

cin>>n;

cout<<"Enter "<<n<<" elements of the arrayay:";

int array[n],i=0;

while(i<n)

{

cin>>array[i];

i++;

}

selectionSort(array, n);

cout<<"The sorted elements of the arrayay are: ";

print(array, n);

return 0;

}

output:

Enter size of the arrayay:10
Enter 10 elements of the arrayay:7 3 4 9 7 11 15 10 7 8
The sorted elements of the arrayay are:
3 4 7 7 7 8 9 10 11 15


Process exited normally.
Press any key to continue . . .