Bubble Sort

#include<stdio.h>

int main(){
int a[]={5,1,4,3,2};
int i,k,m,tmp,len = 5;
for(m=0;m<len;m++)
printf("%d ",a[m]);
printf("\n");

for(i=0;i<len;i++)
for(k=0;k<len-1;k++){

if(a[k]>a[k+1]){

tmp = a[k];
a[k]=a[k+1];
a[k+1]=tmp;
printf("Round %d : Swap [%d,%d] ",i+1,a[k],a[k+1]);
for(m=0;m<len;m++)
printf("%d ",a[m]);
printf("\n");
}
}
for(m=0;m<len;m++)
printf("%d ",a[m]);
return 0;
}

No comments:

Post a Comment