/* Include lib */
#include <stdio.h>
#include <assert.h>
/* Define */
#define ARRAY_MAX 4
#define ARRAY_INPUT { 5, 7, 0, 1}
int array_input[ARRAY_MAX] = ARRAY_INPUT;
/* API sort array */
int sort_array_from_small(int *arr, int arr_size)
{
int j, tmp, end = arr_size;
/*
* There are two loop:
* Small loop will go from start to (size - 1), compare a element and next element
* then move larger number to right. Continue with next element and its next one.
* Finally, largest number will go to right of array.
*
* Big loop will do small loop with ignoring last right element (biggest value).
* Then it will move second largest number to the end, before biggest value above.
*/
while (end) {
for (j = 0; j < end - 1; j++) {
if (arr[j] > arr[j + 1]) {
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
end--;
}
return 0;
}
/* Main func interract with user */
int main(void)
{
int i;
printf("Array input : ");
for (i = 0; i < ARRAY_MAX; i++)
printf("%d ", array_input[i]);
printf("\n");
/* Sort array */
sort_array_from_small(&array_input[0], ARRAY_MAX);
/* Output */
printf("Array output: ");
for (i = 0; i < ARRAY_MAX; i++)
printf("%d ", array_input[i]);
printf("\n");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: