C Code 5 _ To sort the given elements in ascending order using Selection Sort


C Code 5 _ To sort the given elements in ascending order using Selection Sort


Program :


#include<stdio.h>
int main()
{
    int a[50];
    int i,j,n,temp;
    printf(" * * * * * * * * * * * Selection Sort * * * * * * * * * * * * * *");
    printf("\n\nEnter the number of elements to to sorted : ");
    scanf("%d",&n);
    printf("\n\nEnter %d elements : ",n);
    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    printf("\n\nSorted array is : ");
    for(i=0;i<n-1;i++)
    {
         for(j=i+1;j<n;j++)
         {
               if(a[i]>a[j])
               {
                     temp=a[i];
                     a[i]=a[j];
                     a[j]=temp;
               }
         }
    }
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }
    getch();
    return 0;
}




Output:









Thank you ...

Previous Post
Next Post
Related Posts

0 comments:

Popular Posts