bubble sort in C language
Bubble sort is Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
if you want to sort it from the smallest to the biggest
#include<stdio.h>
int main()
{
int a,b,temp;
int array[12]={55,17,87,94,78,88,60,93,29,68,51};\
for(a=0;a<11;a++)
{
for(b=0;b<11-a;b++)
{
if(array[b]>array[b+1])
{
temp=array[b];
array[b]=array[b+1];
array[b+1]=temp;
}
}
}
for(a=1;a<12;a++)
{
printf(" %d",array[a]);
}
return 0;
}
or if you want to sort it from the biggest to the smallest
#include<stdio.h>
int main()
{
int a,b,temp;
int array[12]={55,17,87,94,78,88,60,93,29,68,51};\
for(a=0;a<11;a++)
{
for(b=0;b<11-a;b++)
{
if(array[b]<array[b+1])
{
temp=array[b];
array[b]=array[b+1];
array[b+1]=temp;
}
}
}
for(a=0;a<11;a++)
{
printf(" %d",array[a]);
}
return 0;
}
that is all from me
thank you
if you want to sort it from the smallest to the biggest
#include<stdio.h>
int main()
{
int a,b,temp;
int array[12]={55,17,87,94,78,88,60,93,29,68,51};\
for(a=0;a<11;a++)
{
for(b=0;b<11-a;b++)
{
if(array[b]>array[b+1])
{
temp=array[b];
array[b]=array[b+1];
array[b+1]=temp;
}
}
}
for(a=1;a<12;a++)
{
printf(" %d",array[a]);
}
return 0;
}
or if you want to sort it from the biggest to the smallest
#include<stdio.h>
int main()
{
int a,b,temp;
int array[12]={55,17,87,94,78,88,60,93,29,68,51};\
for(a=0;a<11;a++)
{
for(b=0;b<11-a;b++)
{
if(array[b]<array[b+1])
{
temp=array[b];
array[b]=array[b+1];
array[b+1]=temp;
}
}
}
for(a=0;a<11;a++)
{
printf(" %d",array[a]);
}
return 0;
}
that is all from me
thank you
Comments
Post a Comment