Write a program to sort an array in C with detailed explanation

 




//WAP to sort the array
//input 7,4,9,2. output 2 4 7 9
//Steps 1:first use conio.h Library and take some variables as much required
//step2:Now take array of required size or above required size
//step3:Take input how many elements user will input
//step4'Now use for loop to insert elements into array
//step5:now use two for loop and compare first and second elements of the array at a time if condition is true then swap both elements by uts position
//step6:This loop will until number of elements in the array
//step7:Finally using for loop print array in ascending order

#include<conio.h>

int main()
{
    int I,j,n,array[10],temp;
    printf("Enter how many numbers you want to sort:");
    scanf("%d",&n);
    for(I=0;I<n;I++)
    {
        printf("Enter an element:");
        scanf("%d",&array[I]);
    }
    for(I=0;I<n-1;I++)
    {
       
        for(j=I+1;j<n;j++)
        {
           
            if(array[I]>array[j])
            {
                temp=array[I];
                array[I]=array[j];
                array[j]=temp;
            }
           
        }
       
    }
    printf("Elements in ascending order is:\n");
    for(I=0;I<n;I++)
    {
        printf("%d",array[I]);
     }
   
   
    return 0;
       
   
           
       
}
    
    


No comments:

Powered by Blogger.