C Program to sort an Array Using Bubble Sort



Bubble Sort in C is a sorting algorithm where we repeatedly iterate through the array and swap adjacent elements that are unordered.



  1. //C Program to sort an array using Bubble Sort
  2. #include<stdio.h>
  3. int main()
  4. {
  5. int a[100],n,i,j,temp;
  6. printf("Enter the size of array: ");
  7. scanf("%d",&n);
  8. printf("Enter the array elements: ");
  9. for(i=0;i<n;++i)
  10. {
  11. scanf("%d",&a[i]);
  12. }
  13. for(i=1; i<n; i++)
  14. {
  15. for(j=0; j<(n-i); j++)
  16. {
  17. if(a[j]>a[j+1])
  18. {
  19. temp=a[j];
  20. a[j]=a[j+1];
  21. a[j+1]=temp;
  22. }
  23. }
  24. }
  25. printf("Array after sorting: ");
  26. for(i=0; i<n; i++)
  27. {
  28. printf("%d ",a[i]);
  29. }
  30. return 0;
  31. }

Output


Output!