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