SELECTION SORT using C programming

Image result for c programming

SELECTION SORT using C programming
Code:
  #include<stdio.h>
int main(){
  int a[20],n,i;
  int j,temp=0,k;
  printf("Enter the number of elements in the array\n");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    printf("Enter element %d\n",i);
    scanf("%d",&a[i]);
  }
  printf("Selection sort.\n");
  printf("array before sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  for(i=1;i<=n;i++)
  {
    for(j=i+1;j<=n;j++)
    {
      if(a[i]>a[j])
      {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
      }
    }
    printf("\nAfter Iteration %d\n",i);
      for(k=1;k<=n;k++)
      {
        printf("%d ",a[k]);
      }
  }
  printf("\narray after sorting:\n");
  for(i=1;i<=n;i++)
  {
    printf("%d ",a[i]);
  }
  return 0;
}

No comments