Q:

Write C program to sort an array in ascending order

0

Write C program to sort an array in ascending order

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

int main()
{
    int arr[100];
    int size, i, j, temp;

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

    //Reading elements of array
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }
    //Sorting an array in ascending order
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            //If there is a smaller element found on right of the array then swap it.
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    //Printing the sorted array in ascending order
    printf("\nElements of array in sorted ascending order:\n");
    for(i=0; i<size; i++)
    {
        printf("%d\n", arr[i]);
    }

    return 0;
}

Result:

Enter size of array: 10

Enter elements in array: 10

30

50

60

70

40

80

90

20

100

Elements of array in sorted ascending order:

10

20

30

40

50

60

70

80

90

100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to copy all elements of one array ... >>
<< Write C program to print all unique element in an ...