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

the following question should be answered in the C language: The code for the Se

ID: 3816394 • Letter: T

Question

the following question should be answered in the C language:

The code for the Selection Sort algorithm is as follows:

for(i=0; i<n; i++) {

small = i;

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

   if(x[j] < x[small])

    small = j;       

swap(i, small);

    }   

}

Where swap(arg1, arg2) swaps the position of arg1 with that of arg2. The integer n represents the size of the array. You will need to write the user-defined function swap(). Implement the code above in a program and sort the following integer array:

int a[] = {4, 1, 9, 5, 7, 3, 8, 0, 6, 2};

Explanation / Answer

//The swap() function and program with output is given below:

// The change in above code is, instead of swapping i and small index we have to swap address of the value where it is stored in memory. therefore we used swap(&x[i],&x[small]);

#include <stdio.h>
void swap(int *arg1,int *arg2)
{
    int temp;
    temp=*arg1;
    *arg1=*arg2;
    *arg2=temp;
}
int main()
{
    int x[]={4,1,9,5,7,3,8,0,6,2};
    int i,j,n,small;
    n=10;
    for(i=0;i<n;i++)
    {
        small=i;
        for(j=i+1;j<n;j++)
        {
            if(x[j]<x[small])
            small=j;
            swap(&x[i],&x[small]);
        }
  
    }
for(i=0;i<n;i++)
printf("%d ",x[i]);
return 0;
}

/*output

    0  1  2  4  5  6  3  8  7  9  

sh-4.2$