fork download
  1. //Basic Bubble Sort
  2. #include<stdio.h>
  3.  
  4. int main(){
  5. int a[]={5,1,4,3,-1};//Data
  6. int i,k,m,tmp,len = 5;//Length of Data
  7.  
  8. for(m=0;m<len;m++)
  9. printf("%d ",a[m]);
  10. printf("\n");
  11.  
  12. for(i=0;i<len;i++)
  13. for(k=0;k<len-1;k++){
  14. if(a[k]>a[k+1]){
  15. tmp = a[k];
  16. a[k]=a[k+1];
  17. a[k+1]=tmp;
  18. printf("Round %d : Swap [%d,%d] ",i+1,a[k],a[k+1]);//show step
  19. for(m=0;m<len;m++)//show data in array
  20. printf("%d ",a[m]);
  21. printf("\n");
  22. }
  23. }
  24. for(m=0;m<len;m++)
  25. printf("%d ",a[m]);
  26. return 0;
  27. }
Success #stdin #stdout 0s 2724KB
stdin
Standard input is empty
stdout
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