How to write a C Program to sort an array using bubble sort in C Programming Language ?
This C Program to sort an array using bubble sort.
Solution:
- #include<stdio.h>
- main()
- {
- int n,i,j,temp;
- printf("Enter the number of elements: ");
- scanf("%d",&n);
- int a[n];
- printf("Enter the elements in the array\n");
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- for(i=0;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("\nThe sorted array is - \n" );
- for(i=0;i<n;i++)
- printf("%d, ",a[i]);
- }