Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// sorting the elements of array using bubble sort method

#include<stdio.h>

int main()
{
int a[5]={5,4,3,2,1};
int i;
int j;
int temp;

printf("\nArray elements before sorting:\n ");


for(i=0;i<5;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}

for(i=5;i>1;i--)
{
for(j=0;j<(i-1);j++) // i-1 condition is because the numbers
already shifted
// to right doesn't need to be compared
again
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}

printf("\nArray after sorting: \n");


for(j=0;j<5;j++)
{
printf("a[%d] = %d",i,a[j]);
printf("\n");
}

return 0;

You might also like