Bubble Sort
output:
enter the elements in array
8
9
25
8
51
3
66
23
95
75
Before Sorting 8 9 25 8 51 3 66 23 95 75
After Sorting 3 8 8 9 23 25 51 66 75 95
-----------------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int arr[20];
int i,j,n,temp = 0;
printf("\n enter the elements in array \n");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Before Sorting ");
for(i=0;i<10;i++)
{
printf("%d ",arr[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n");
printf("After Sorting ");
for(i=0;i<10;i++)
{
printf("%d ",arr[i]);
}
}
output:
enter the elements in array
8
9
25
8
51
3
66
23
95
75
Before Sorting 8 9 25 8 51 3 66 23 95 75
After Sorting 3 8 8 9 23 25 51 66 75 95
-----------------------------------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int arr[20];
int i,j,n,temp = 0;
printf("\n enter the elements in array \n");
for(i=0;i<10;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Before Sorting ");
for(i=0;i<10;i++)
{
printf("%d ",arr[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n");
printf("After Sorting ");
for(i=0;i<10;i++)
{
printf("%d ",arr[i]);
}
}
No comments :
Post a Comment