//Basic Bubble Sort
#include<stdio.h>
int main(){
int a[]={5,1,4,3,-1};//Data
int i,k,m,tmp,len = 5;//Length of Data
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]);//show step
for(m=0;m<len;m++)//show data in array
printf("%d ",a[m]);
printf("\n");
}
}
for(m=0;m<len;m++)
printf("%d ",a[m]);
return 0;
}
Ly9CYXNpYyBCdWJibGUgU29ydAojaW5jbHVkZTxzdGRpby5oPgoKaW50IG1haW4oKXsKCWludCBhW109ezUsMSw0LDMsLTF9Oy8vRGF0YQoJaW50IGksayxtLHRtcCxsZW4gPSA1Oy8vTGVuZ3RoIG9mIERhdGEKCglmb3IobT0wO208bGVuO20rKykKCQlwcmludGYoIiVkICIsYVttXSk7CglwcmludGYoIlxuIik7CgkKCWZvcihpPTA7aTxsZW47aSsrKQoJCWZvcihrPTA7azxsZW4tMTtrKyspewoJCQlpZihhW2tdPmFbaysxXSl7CgkJCQl0bXAgPSBhW2tdOwkKCQkJCWFba109YVtrKzFdOwoJCQkJYVtrKzFdPXRtcDsKCQkJCXByaW50ZigiUm91bmQgJWQgOiBTd2FwIFslZCwlZF0gIixpKzEsYVtrXSxhW2srMV0pOy8vc2hvdyBzdGVwCgkJCQkJZm9yKG09MDttPGxlbjttKyspLy9zaG93IGRhdGEgaW4gYXJyYXkKCQkJCQkJcHJpbnRmKCIlZCAiLGFbbV0pOwoJCQkJCXByaW50ZigiXG4iKTsKCQkJfQoJCX0KCQkJZm9yKG09MDttPGxlbjttKyspCgkJCQlwcmludGYoIiVkICIsYVttXSk7CnJldHVybiAwOwkKfQ==
5 1 4 3 -1
Round 1 : Swap [1,5] 1 5 4 3 -1
Round 1 : Swap [4,5] 1 4 5 3 -1
Round 1 : Swap [3,5] 1 4 3 5 -1
Round 1 : Swap [-1,5] 1 4 3 -1 5
Round 2 : Swap [3,4] 1 3 4 -1 5
Round 2 : Swap [-1,4] 1 3 -1 4 5
Round 3 : Swap [-1,3] 1 -1 3 4 5
Round 4 : Swap [-1,1] -1 1 3 4 5
-1 1 3 4 5