Sort array elements in ascending or descending order with C.
#include <stdio.h>
int main()
{
int arr[100];
int size;
int i, j, temp;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
/* Print the sorted array */
printf("\nElements of array in ascending order: ");
for (i = 0; i < size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
Output
Enter size of array : 10
Enter elements in array : 1 2 4 5 3 7 8 9 6 11
Elements of array in ascending order : 1 2 3 4 5 6 7 8 9 11
0 Comments