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

Implement the sort procedure from Assignment 18 as a void function. Pass the add

ID: 3856947 • Letter: I

Question

Implement the sort procedure from Assignment 18 as a void function. Pass the address of the first array element to the function along with the number of elements in the array. Use pointers in your function to reference the array elements. The sort function should do nothing but sort the elements of the array. DO NOT pass the entire array as an argument to the function. As in Assignment 18, print out the array before and after sorting. Use the same test data. All output should be in main(), not in the function. Prog18:Consider the following sort procedure written in pseudo code: Exchange Sort of Array X[] For I = 0 to (number of array elements - 2), increment by 1 For J = (I + 1) to (number of array elements - 1), increment by 1 If X[I] is greater than X[J] then temp = X[I] X[I] = X[J] X[J] = temp Next J Next I Implement the sort procedure in main() without using pointers. Declare the array as follows using the test data shown: int x[] = { 28, 87, -3, 45, 19 }; Use a for loop to print out the array before sorting and again after sorting. There should be no output within the sort procedure.

Explanation / Answer

Here is the program in C

---------------------------------------

#include <stdio.h>

void exchange_sort(int *p, int n)

{

int i;

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

{

int j;

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

{

if(*(p+i) > *(p+j))

{

int temp = *(p+i);

*(p+i) = *(p+j);

*(p+j) = temp;

}

}

}

}

int main(void) {

int a[10]={23,-34,56,1,2,67,-28,100,5,-6};

printf("Array before sorting ");

int i;

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

printf("%d ",a[i]);

exchange_sort(&a[0],10);

printf(" Array after sorting ");

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

printf("%d ",a[i]);

return 0;

}

Output

------------------

Array before sorting
23 -34 56 1 2 67 -28 100 5 -6
Array after sorting
-34 -28 -6 1 2 5 23 56 67 100