Write a function that takes an array and its size as inputs, and then sorts the
ID: 3814697 • Letter: W
Question
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; 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). Swap the smallest value found in the array with the value at index i; Increment i by one; 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);Explanation / Answer
libsort.h:
#include <stdio.h>
void sort(int array[],int size);
libsort.c
void sort(int array[],int size)
{
int i,temp,min,j;
for(i=0;i<size;i++)
{
min = i;
for(j=i+1;j<size;j++)
{
if(array[j]<array[min])
{
min=j;
}
}
temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.