fork download
  1. #include<stdio.h>
  2.  
  3. void sort_b(int data1[], int n)
  4. {
  5. int i, j, tmp;
  6. int count_b = 0;
  7. for(i = 0; i < n; i++){
  8. for(j = n - 1; j > i; j--){
  9. if(data1[j] < data1[j-1]){
  10. tmp = data1[j];
  11. data1[j] = data1[j-1];
  12. data1[j-1] = tmp;
  13. count_b++;
  14. }
  15. }
  16. }
  17.  
  18. for(i = 0; i < n; i++)
  19. printf("%d\n",data1[i]);
  20. printf("バブルソートの交換回数 -> %d回\n\n",count_b);
  21. }
  22.  
  23. void sort_s(int data2[], int n)
  24. {
  25. int i, j, tmp;
  26. int count_s = 0;
  27.  
  28. for(i = 0; i < n; i++){
  29. int max = i;
  30. for(j = i+1; j < n; j++)
  31. if(data2[j] > data2[max])max = j;
  32.  
  33. tmp = data2[i];
  34. data2[i] = data2[max];
  35. data2[max] = tmp;
  36. count_s++;
  37. }
  38.  
  39. for(i = 0; i < n; i++)
  40. printf("%d\n",data2[i]);
  41. printf("選択ソートの交換回数 -> %d回\n",count_s);
  42.  
  43. }
  44.  
  45. int main()
  46. {
  47. FILE *fp;
  48. int data[20], i = 0;
  49.  
  50. fp = fopen("data.txt","r");
  51. if(fp == NULL){
  52. puts("open error!!");
  53. return -1;
  54. }
  55.  
  56. while(fscanf(fp,"%d",&data[i]) != EOF)i++;
  57.  
  58. sort_b(data,i);
  59. sort_s(data,i);
  60.  
  61. fclose(fp);
  62.  
  63. return 0;
  64. }
  65.  
Runtime error #stdin #stdout 0.02s 1808KB
stdin
Standard input is empty
stdout
open error!!