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