1. Bubble Sort
-> Bubble sort is a type of sorting.
-> It is used for sorting 'n' (number of items) elements.
-> It compares all the elements one by one and sorts them based on their values.
-> In the above diagram, element 40 is greater than 10, so these values must be swapped. This operation continues until the array is sorted in ascending order.
/* C program to implement Bubble Sort */
#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
for(j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("Elements after sorting:\n");
for(i=0; i<n; i++)
printf("%d ",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter 5 numbers:40 10 20 30 50
Elements after sorting:
10 20 30 40 50
-> Bubble sort is a type of sorting.
-> It is used for sorting 'n' (number of items) elements.
-> It compares all the elements one by one and sorts them based on their values.
-> In the above diagram, element 40 is greater than 10, so these values must be swapped. This operation continues until the array is sorted in ascending order.
/* C program to implement Bubble Sort */
#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
for(j=0; j<n-1-i; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("Elements after sorting:\n");
for(i=0; i<n; i++)
printf("%d ",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter 5 numbers:40 10 20 30 50
Elements after sorting:
10 20 30 40 50