1
0

sorting_main.c~ 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //------------------------------------------------------------
  2. // Sorting_main
  3. // establish the main (menu driven) program to help with
  4. // project 1.
  5. //
  6. // compile: gcc -Wall -Werror -O3 sorting.c sorting_main.c -o proj1
  7. //------------------------------------------------------------
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/time.h>
  11. #include <sys/resource.h>
  12. #include <time.h>
  13. #include "sorting.h"
  14. #ifndef CLOCKS_PER_SEC
  15. #define CLOCKS_PER_SEC 1000000
  16. #endif
  17. #define MAXFILELEN (50) // length of string (path and filename)
  18. // functions
  19. int main (
  20. int Argc,
  21. char **Argv)
  22. {
  23. // main program - menu for functions with using database
  24. double cstart, cend;
  25. int Response = 0;
  26. int Saved = 0;
  27. int Size = 0;
  28. double N_Comp = 0;
  29. double N_Move = 0;
  30. long *Array = NULL;
  31. char Filename[MAXFILELEN] = "";
  32. while (1)
  33. {
  34. printf("\n");
  35. printf("TEST MENU\n");
  36. printf("1. Load array from file\n");
  37. printf("2. Save array to file\n");
  38. printf("3. Shell Sort (Insertion)\n");
  39. printf("4. Improved Bubble Sort\n");
  40. printf("5. Exit\n");
  41. printf("Enter your choice: ");
  42. scanf("%d",&Response);
  43. getchar();
  44. if (Response == 5) // quit program
  45. {
  46. if (Array != NULL)
  47. {
  48. printf("Removing and deallocating array\n");
  49. free((void *)Array);
  50. Array = NULL;
  51. printf("done!\n");
  52. }
  53. return (OK);
  54. }
  55. if (Response == 1) // load file
  56. {
  57. if (Array != NULL)
  58. {
  59. printf("\nRemoving and deallocating array\n");
  60. free((void *)Array);
  61. Array = NULL;
  62. printf("done!\n");
  63. }
  64. printf("\nEnter input file (including path): ");
  65. scanf("%s", Filename);
  66. Array = Load_File (Filename, &Size);
  67. if (Size <= 0)
  68. {
  69. printf("\nError in inputs, file not loaded..\n");
  70. if (Array != NULL)
  71. {
  72. printf("Removing and deallocating array\n");
  73. free((void *)Array);
  74. Array = NULL;
  75. printf("done!\n");
  76. }
  77. }
  78. else
  79. {
  80. printf("\nLoaded %d long integers\n", Size);
  81. }
  82. }
  83. if (Response == 2) // save file
  84. {
  85. if (Array == NULL)
  86. {
  87. printf("\nMust load in data from file (option 1) before saving one!\n");
  88. }
  89. else
  90. {
  91. printf("\nEnter output file (including path): ");
  92. scanf("%s", Filename);
  93. Saved = Save_File (Filename, Array, Size);
  94. if (Saved != Size)
  95. {
  96. printf("Error in saving! Only %d out of %d long integers saved\n",
  97. Saved, Size);
  98. }
  99. else
  100. {
  101. printf("Saved all %d long integers\n", Saved);
  102. }
  103. }
  104. }
  105. if ( (Response > 2)
  106. && (Response < 5))
  107. {
  108. if (Array == NULL)
  109. {
  110. printf("\nMust load in data from file (option 1) before sorting one!\n");
  111. }
  112. else
  113. {
  114. // initialize time function
  115. cstart = (double) clock();
  116. // initialize numbers of comparisons and moves
  117. N_Comp = 0;
  118. N_Move = 0;
  119. switch(Response)
  120. {
  121. case 3:
  122. printf("Sorting by Shell Sort (Insertion)\n");
  123. Shell_Insertion_Sort (Array, Size, &N_Comp, &N_Move);
  124. break;
  125. case 4:
  126. printf("Sorting by Improved Bubble Sort\n");
  127. Improved_Bubble_Sort (Array, Size, &N_Comp, &N_Move);
  128. break;
  129. }
  130. // print results: Time, N_Comp, N_Move
  131. cend = (double) clock();
  132. printf("\n");
  133. printf(" Elapsed Time (sec): %f\n", (cend - cstart)/CLOCKS_PER_SEC);
  134. printf(" # Comparisons: %f\n", N_Comp);
  135. printf(" # Moves: %f\n", N_Move);
  136. }
  137. }
  138. }
  139. return (OK);
  140. } // main()