Follow Us

LightBlog

Breaking

Wednesday, March 1, 2017

Write a c program for selection sort ?

Write a c program for selection sort ?
#include<stdio.h>
int main()
{

  int n,i,j,temp,array[20];

  printf("Enter total elements: ");
  scanf("%d",&n);

  printf("Enter %d elements: ",n);
  for(i=0;i<n;i++)
      scanf("%d",&array[i]);

  for(i=0;i<n;i++)
{
      for(j=i+1;j<n;j++)
{
           if(array[i]>array[j])
{
               temp=array[i];
              array[i]=array[j];
              array[j]=temp;
           }
      }
  }

  printf("After sorting is: ");
  for(i=0;i<n;i++)
      printf(" %d",array[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is:  0 4 5 7 21

No comments:

Post a Comment