sorting.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "sorting.h"
  5. long *Load_File(char * Filename, int *Size){
  6. // num ints in file
  7. int val0 = 0;
  8. // open file for reading
  9. FILE * fptr = NULL;
  10. fptr = fopen(Filename, "r");
  11. if (fptr == NULL){ // check for error
  12. printf("File name error.\n");
  13. return NULL;
  14. }
  15. if(fscanf(fptr, "%d", &val0) == 0){ // check for error
  16. printf("File read error.\n");
  17. return NULL;
  18. }
  19. // scan values into buffer (return value)
  20. long *buffer = malloc(sizeof(long) * val0); // malloc return value
  21. if (buffer == NULL){
  22. printf("Buffer malloc error.\n");
  23. return NULL;
  24. }
  25. int i;
  26. for (i = 0; i < val0; i++){
  27. if(fscanf(fptr, "%ld", &buffer[i]) == 0){
  28. printf("File read error.\n");
  29. return NULL; // stop on error
  30. }
  31. }
  32. // clean up
  33. *Size = val0; // assign size
  34. if(!fclose(fptr) == 0){ // close file
  35. printf("File close error.");
  36. return NULL; // return NULL on file close error
  37. }
  38. return buffer;
  39. }
  40. int Save_File(char *Filename, long *Array, int Size){
  41. // count of successful writes (return value)
  42. int count = 0;
  43. // write to file
  44. FILE * fptr = NULL;
  45. fptr = fopen(Filename, "w");
  46. if(fprintf(fptr, "%d\n", Size) == 0){
  47. return EXIT_FAILURE;
  48. }
  49. int i;
  50. for(i = 0; i < Size; i++){
  51. count = fprintf(fptr, "%ld\n", Array[i]);
  52. }
  53. // close file
  54. if(!fclose(fptr) == 0){
  55. printf("File close error.");
  56. return EXIT_FAILURE; // return NULL on file close error
  57. }
  58. return count;
  59. }
  60. void Shell_Insertion_Sort(long *Array, int Size, double *N_Comp, double *N_Move){
  61. // generate sequence
  62. int maxInd = 0;
  63. int * pratt = genSequence(Size);
  64. while(pratt[maxInd] != 0){ // calculate length of sequence
  65. maxInd++;
  66. }
  67. // declare and initialize variables
  68. int cCount = 0; // comp count
  69. int mCount = 0; // move count
  70. long value = 0; // temp value
  71. int gapInc = 0;
  72. int i = 0;
  73. int j = 0;
  74. int gap = 0;
  75. // increment through sequence array
  76. for(gapInc = maxInd; gapInc >= 0; gapInc--){
  77. gap = pratt[gapInc]; // makes code simpler
  78. for(i = gap; i < Size; i++){ // create subarrays
  79. j = i;
  80. // insertion sort
  81. value = Array[i];
  82. mCount++; // value = Array[i]
  83. cCount++; // value < Array[j - gap]
  84. while (j >= gap && value < Array[j - gap]){
  85. cCount++; // value < Array[j - gap]
  86. Array[j] = Array[j - gap];
  87. mCount++; // Array[j] = Array[j - gap]
  88. j = j - gap;
  89. }
  90. Array[j] = value;
  91. mCount++; // Array[j] = value
  92. }
  93. }
  94. // clean up
  95. free(pratt); // free resources
  96. *N_Comp = cCount; // set counters
  97. *N_Move = mCount;
  98. }
  99. void Shell_Selection_Sort(long *Array, int Size, double *N_Comp, double *N_Move){
  100. // generate sequence
  101. int maxInd = 0;
  102. int * pratt = genSequence(Size);
  103. while(pratt[maxInd] != 0){ // calculate length of sequence
  104. maxInd++;
  105. }
  106. // declare and initialize variables
  107. int cCount = 0; // comp count
  108. int mCount = 0; // move count
  109. long value = 0; // temp value
  110. int gapInc = 0;
  111. int i = 0;
  112. int j = 0;
  113. int gap = 0;
  114. int minIndex = 0;
  115. // increment through sequence array
  116. for(gapInc = maxInd; gapInc >= 0; gapInc--){
  117. gap = pratt[gapInc]; // makes code simpler
  118. for(i = gap; i < Size - 1; i++){ // create subarrays
  119. // selection sort
  120. minIndex = i;
  121. for (j = i + 1; j < Size; j++){ // find global minimum
  122. if (Array[minIndex] > Array[j]){
  123. minIndex = j; // new min
  124. }
  125. cCount++; // Array[minIndex] > Array[j]
  126. }
  127. // swap values with temp
  128. value = Array[i];
  129. mCount++; // value = Array[i]
  130. Array[i] = Array[minIndex];
  131. mCount++; // Array[i] = Array[minIndex]
  132. Array[minIndex] = value;
  133. mCount++; // Array[minIndex] = value
  134. }
  135. }
  136. // clean up
  137. free(pratt); // free sequence
  138. *N_Comp = cCount; // set counter
  139. *N_Move = mCount;
  140. }
  141. // (2^p * 3^q)
  142. int * genSequence(int maxVal){
  143. // count number of rows needed (space required for allocation)
  144. int numRows = 0; // number of triangle rows needed
  145. int val = 1; // used to count num rows
  146. while(val < maxVal){
  147. numRows++;
  148. val = val * 3; // right side of triangle
  149. }
  150. // memory allocation
  151. int arraySize = 0; // space to alloc for sequence
  152. int i;
  153. for(i = numRows; i > 0; i--){
  154. arraySize = arraySize + i; // num values in each row
  155. }
  156. int * pratt = calloc(arraySize + 1, sizeof(int)); // add 1 for safety
  157. // initialize values
  158. int currInd = 0; // track current index of array
  159. int currRow = 0; // track row of triangle
  160. int j; // incrementor
  161. for(currRow = 0; currRow < numRows; currRow++){
  162. for(j = 0; j <= currRow; j++){
  163. pratt[currInd] = 1 * genHelp(2, currRow - j) * genHelp(3, j);
  164. currInd++; // cycle through array with proper numbers
  165. }
  166. }
  167. return pratt; // return array
  168. }
  169. // helper function for sequence generation
  170. // raises base to exponent power, returns only positive integers
  171. int genHelp(int base, int exponent){
  172. if(exponent <= 0){
  173. return 1;
  174. }
  175. if(exponent == 1){
  176. return base;
  177. }
  178. int retval = 1;
  179. int i;
  180. for(i = 0; i < exponent; i++){
  181. retval = retval * base;
  182. }
  183. return retval;
  184. }
  185. int Print_Seq(char *Filename, int maxVal){
  186. // generate sequence
  187. int * sequence = genSequence(maxVal);
  188. // open file for writing
  189. FILE * fptr = NULL;
  190. fptr = fopen(Filename, "w");
  191. // calculate length of array
  192. int length = 0;
  193. while(sequence[length] != 0){
  194. length++;
  195. }
  196. fprintf(fptr, "%d\n", length); // print length first
  197. // print values to file
  198. int i;
  199. for(i = 0; i < length; i++){
  200. fprintf(fptr, "%d\n", sequence[i]);
  201. }
  202. // close file
  203. if(!fclose(fptr) == 0){
  204. printf("File close error.");
  205. return EXIT_FAILURE; // return NULL on file close error
  206. }
  207. // clean up
  208. free(sequence); // release memory
  209. return length; // return specified value
  210. }