Question: Q1) Write a function that takes an array and its size as inputs, and then sorts the elements of the array in ascending order. For

Q1) Write a function that takes an array and its size as inputs, and then sorts the elements of the array in ascending order. For example, if the array contains the values (12, 3, 5, 4, 7), then the array sorted in ascending order will be (3, 4, 5, 7, 12).

In order to sort the array, you must use the following algorithm: (0) Initialize i = 0; (1) Starting at array index i, find the smallest value in the array at or after index i (i.e., any index greater than or equal to i within the proper array bounds). (2) Swap the smallest value found in the array with the value at index i; (3) Increment i by one; (4) If i is less than the array size, then repeat the process starting at step (1).

You must use the following function prototype:

 void sort(int array[], int size); 

Note: You must write your function prototype in a header file named libsort.h and you must write your function definition in a source file named libsort.c. We are providing you the main source file sort.c, which you can use to test your function. Do not change any- thing in sort.c.

#include #include #include "libsort.h"

int main() { int* array; int size, c;

printf("Enter the array size: "); scanf("%d", &size);

array = (int*) malloc(size * sizeof(int));

printf("Enter %d integers: ", size);

for (c = 0; c < size; c++) scanf("%d", &array[c]);

sort(array, size);

printf("Array sorted in ascending order: "); for (c = 0; c < size; c++) printf("%d ", array[c]);

printf(" "); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!