Implementation of Bubble sort.
Source Code:
#include<stdio.h>
int main(){
int ary[10],n,i,j,temp;
printf("Enter size of Array:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(ary[j]>ary[j+1]){
temp = ary[j];
ary[j] = ary[j+1];
ary[j+1] = temp;
}
}
}
printf("After sorting, Array is:\n");
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
int main(){
int ary[10],n,i,j,temp;
printf("Enter size of Array:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(ary[j]>ary[j+1]){
temp = ary[j];
ary[j] = ary[j+1];
ary[j+1] = temp;
}
}
}
printf("After sorting, Array is:\n");
for(i=0;i<n;i++)
printf("%d ",ary[i]);
return 0;
}
Enter size of Array:6
Enter 6 elements:
8 3 4 6 2 1
After sorting, Array is:
1 2 3 4 6 8
Enter 6 elements:
8 3 4 6 2 1
After sorting, Array is:
1 2 3 4 6 8