sorting.c~ 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "sorting.h"
  5. Node * Load_File(char * Filename){
  6. // temporarily store last read value
  7. long val = 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. Node *head;
  16. head = malloc(sizeof(Node));
  17. head->value = 0;
  18. head->next = NULL;
  19. Node *temp = head;
  20. while(fscanf(fptr, "%ld", &val) != EOF){ // check for error
  21. temp->value = val;
  22. temp->next = malloc(sizeof(Node));
  23. temp = temp->next;
  24. temp->value = 0;
  25. temp->next = NULL;
  26. }
  27. // clean up
  28. if(!fclose(fptr) == 0){ // close file
  29. printf("File close error.");
  30. return NULL; // return NULL on file close error
  31. }
  32. return head;
  33. }
  34. int Save_File(char * Filename, Node * list){
  35. int count = 0;
  36. // write to file
  37. FILE * fptr = NULL;
  38. fptr = fopen(Filename, "w");
  39. Node *temp = list;
  40. while(temp->next != NULL){
  41. fprintf(fptr, "%ld\n", temp->value);
  42. count++;
  43. temp = temp->next;
  44. }
  45. // close file
  46. if(!fclose(fptr) == 0){
  47. printf("File close error.");
  48. return EXIT_FAILURE; // return NULL on file close error
  49. }
  50. return count;
  51. }
  52. Node * Shell_Sort(Node *list){
  53. int size = Get_Size(list);
  54. // generate sequence
  55. int maxInd = 0;
  56. int * pratt = genSequence(size);
  57. while(pratt[maxInd] != 0){ // calculate length of sequence
  58. maxInd++;
  59. }
  60. // declare and initialize variables
  61. int cCount = 0; // comp count
  62. int mCount = 0; // move count
  63. long value = 0; // temp value
  64. int gapInc = 0;
  65. int i = 0;
  66. int j = 0;
  67. int gap = 0;
  68. int minIndex = 0;
  69. // increment through sequence array
  70. for(gapInc = maxInd; gapInc >= 0; gapInc--){
  71. gap = pratt[gapInc]; // makes code simpler
  72. for(i = gap; i < size - 1; i++){ // create subarrays
  73. // selection sort
  74. minIndex = i;
  75. for (j = i + 1; j < size; j++){ // find global minimum
  76. if (Get_Node_At(list, minIndex)->value > Get_Node_At(list, j)-> value){
  77. minIndex = j; // new min
  78. }
  79. cCount++; // Array[minIndex] > Array[j]
  80. }
  81. // swap values with temp
  82. value = Get_Node_At(list, i)->value;
  83. mCount++; // value = Array[i]
  84. Get_Node_At(list, i)->value = Get_Node_At(list, minIndex)->value;
  85. mCount++; // Array[i] = Array[minIndex]
  86. Get_Node_At(list, minIndex)->value = value;
  87. mCount++; // Array[minIndex] = value
  88. }
  89. }
  90. // clean up
  91. free(pratt); // free sequence
  92. return list;
  93. }
  94. int Get_Size(Node * list){
  95. int size = 0;
  96. Node *temp = list;
  97. while(temp->next != NULL){
  98. size++;
  99. temp = temp->next;
  100. }
  101. return size;
  102. }
  103. Node * Get_Node_At(Node *list, int index){
  104. Node *temp = list;
  105. int i;
  106. for(i = 0; i < index; i++){
  107. temp = temp->next;
  108. if(temp == NULL){
  109. printf("Error, index out of bounds.");
  110. return NULL;
  111. }
  112. }
  113. return temp;
  114. }
  115. void Destroy_List(Node * head)
  116. {
  117. Node *temp = head;
  118. while(head != NULL)
  119. {
  120. temp = temp->next;
  121. free(head);
  122. head = temp;
  123. }
  124. }
  125. // (2^p * 3^q)
  126. int * genSequence(int maxVal){
  127. // count number of rows needed (space required for allocation)
  128. int numRows = 0; // number of triangle rows needed
  129. int val = 1; // used to count num rows
  130. while(val < maxVal){
  131. numRows++;
  132. val = val * 3; // right side of triangle
  133. }
  134. // memory allocation
  135. int arraySize = 0; // space to alloc for sequence
  136. int i;
  137. for(i = numRows; i > 0; i--){
  138. arraySize = arraySize + i; // num values in each row
  139. }
  140. int * pratt = calloc(arraySize + 1, sizeof(int)); // add 1 for safety
  141. // initialize values
  142. int currInd = 0; // track current index of array
  143. int currRow = 0; // track row of triangle
  144. int j; // incrementor
  145. for(currRow = 0; currRow < numRows; currRow++){
  146. for(j = 0; j <= currRow; j++){
  147. pratt[currInd] = 1 * genHelp(2, currRow - j) * genHelp(3, j);
  148. currInd++; // cycle through array with proper numbers
  149. }
  150. }
  151. return pratt; // return array
  152. }
  153. // helper function for sequence generation
  154. // raises base to exponent power, returns only positive integers
  155. int genHelp(int base, int exponent){
  156. if(exponent <= 0){
  157. return 1;
  158. }
  159. if(exponent == 1){
  160. return base;
  161. }
  162. int retval = 1;
  163. int i;
  164. for(i = 0; i < exponent; i++){
  165. retval = retval * base;
  166. }
  167. return retval;
  168. }