sorting_main.c 3.7 KB

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