BUBBLE SORT in C Programming

Image result for c programming

BUBBLE SORT in C Programming

Code:
  #include<stdio.h>
int main(){
  int n,a[20],i,temp,j,k,flag;
  printf("Enter the number of elements :\n");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    printf("Enter element %d\n",i+1);
    scanf("%d",&a[i]);
  }
  printf("Unsorted list is :\n");
  for(i=0;i<n;i++)
    printf("%d ",a[i]);
  for(i=0;i<n-1;i++)
  {
    flag=0;
    for(j=0;j<=n-2;j++)
    {
      if(a[j]>a[j+1])
      {
        temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        flag++;
      }
    }
      printf("\nAfter Pass %d elements are :",i+1);
      for(k=0;k<n;k++)
        printf("%d ",a[k]);
    if(flag==0)
      break;
  }
  printf("\nSorted list is :\n");
  for(i=0;i<n;i++)
    printf("%d ",a[i]);
  return 0;
}
  

No comments