fork download
  1. #include <stdio.h>
  2.  
  3. int cmp(int x,int y){
  4. return x>y;
  5. }
  6. void swap(int *x,int *y){
  7. int tmp=*x;
  8. *x=*y;
  9. *y=tmp;
  10. }
  11. int main(void){
  12. int a[]={3,1,4,1,5,9,2,6,5};
  13. size_t n =sizeof a/sizeof a[0];
  14. int swapcount;
  15. int swapset;
  16. for(size_t i=0;i<n-1;++i){
  17. for(size_t j=0;j<n-1-i;++j){
  18. if(cmp(a[j],a[j+1])){
  19. swap(&a[j],&a[j+1]);
  20. swapcount++;
  21. swapset=1;
  22. }
  23. }
  24. if(swapset=0){
  25. break;
  26. }
  27. }
  28. for(size_t i=0; i<n;++i){
  29. printf("%d",a[i]);
  30. }
  31. printf("\n");
  32. printf("%d",swapcount);
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 5352KB
stdin
Standard input is empty
stdout
112345569
10