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.
- //C Program to sort an array using Bubble Sort
-
- #include<stdio.h>
-
- int main()
- {
- int a[100],n,i,j,temp;
-
- printf("Enter the size of array: ");
- scanf("%d",&n);
- printf("Enter the array elements: ");
-
- for(i=0;i<n;++i)
- {
- scanf("%d",&a[i]);
- }
-
- for(i=1; i<n; i++)
- {
- for(j=0; j<(n-i); j++)
- {
- if(a[j]>a[j+1])
- {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- }
-
- printf("Array after sorting: ");
-
- for(i=0; i<n; i++)
- {
- printf("%d ",a[i]);
- }
- return 0;
- }
Output
