Advertisement
blainarm387

Comparing three sorting algorithms

Aug 15th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.47 KB | None | 0 0
  1. /**
  2.  * compare.c
  3.  *
  4.  * Compares the number of steps taken to perform bubble, insertion,
  5.  * and selection sorts by sorting every possible iteration of
  6.  * a number of arrays. Results are written into three corresponding
  7.  * csv files: bubble.c, insertion.c, and selection.c
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15. #include <cs50.h>
  16.  
  17. struct result
  18. {
  19.     int min;
  20.     int max;
  21.     unsigned long long total;
  22. };
  23.  
  24. struct trial
  25. {
  26.     int length;
  27.     struct result bubble;
  28.     struct result insertion;
  29.     struct result selection;
  30. };
  31.  
  32. // prototypes
  33. void run_trials(int start, int end, FILE *bubble, FILE *selection, FILE *insertion);
  34.  
  35. // Array functions
  36. void print_array(int arr[], int n);
  37. void swap(int arr[], int i, int j);
  38. void fill(int arr[], int max);
  39. void copy(int arr1[], int arr2[], int len);
  40. void validate(int arr[], int n, char *name);
  41.  
  42. // Sorting algorithms
  43. int bubble(int arr[], int n);
  44. int insertion(int arr[], int n);
  45. int selection(int arr[], int n);
  46.  
  47. // Result handling
  48. void prep_trial(struct trial *t, int i);
  49. void prep_result(struct result *r);
  50. void prep_file(FILE *fp);
  51. void update_result(struct result *r, int operations);
  52. void update_file(struct result r, int len, int count, FILE *fp);
  53.  
  54. int main(void)
  55. {
  56.     FILE *bub;
  57.     FILE *sel;
  58.     FILE *ins;
  59.    
  60.     // Open the file pointers
  61.     bub = fopen("bubble.csv", "w");
  62.     sel = fopen("selection.csv", "w");
  63.     ins = fopen("insertion.csv", "w");
  64.    
  65.     // Check for errors
  66.     if (bub == NULL
  67.     || sel == NULL
  68.     || ins == NULL) {
  69.         printf("Unable to initialize one or more files. Exiting\n");
  70.         return 1;
  71.     }
  72.    
  73.     // Add the top rows of each csv file
  74.     prep_file(bub);
  75.     prep_file(sel);
  76.     prep_file(ins);
  77.    
  78.     // This was originally supposed to be set up to run several trials
  79.     // of different lengths with different iterators. I may still
  80.     // do that at some point in the future if I don't mind letting
  81.     // the computer churn away for an hour or two
  82.     run_trials(10, 300, bub, sel, ins);
  83.    
  84.     printf("All trials complete. Saving files...");
  85.     fclose(bub);
  86.     fclose(ins);
  87.     fclose(sel);
  88.    
  89.     printf(" Done!\n");
  90.     return 0;
  91. }
  92.  
  93. /**
  94.  * Set up the file's top row of headers
  95.  */
  96. void prep_file(FILE *fp)
  97. {
  98.     fprintf(fp, "Length,Max,Avg,Min\n");
  99. }
  100.  
  101. /**
  102.  * Iterate through all possible combinations of a particular arraa
  103.  * and test the minimum, maximum, and average number of operations
  104.  * required for each sorting algorithm
  105.  */
  106. void run_trials(int start, int end, FILE *bub, FILE *sel, FILE *ins)
  107. {
  108.     for(int i = start; i <= end; i += 10) {
  109.         // Set up all the necessary variables and structures
  110.         int arr[i];         // The array to sort
  111.         int count = 0;      // The number of permutations run so far
  112.         struct trial t;     // Data for each trial
  113.        
  114.         // Set up the trial structure: set all three algorithms'
  115.         // min, max, and total to 0, and set length to i
  116.         prep_trial(&t, i);  
  117.        
  118.         // Fill arr with the values we need to sort
  119.         fill(arr, i);
  120.        
  121.         // Run through all possible permutations of
  122.         // the array, sorting each one and getting the
  123.         // min, max, and average values for each run
  124.         for (int j = 0; j < i; j++) {
  125.             for (int k = 0; k < i - 1; k++) {
  126.                 // increment count so we know what to divide
  127.                 // each sum by to get the avg
  128.                 count++;
  129.                
  130.                 // Swap the correct items to get the current
  131.                 // permutation
  132.                 swap(arr, k, k + 1);
  133.                
  134.                 /**
  135.                  * Go through each algorithm and run it; the functions
  136.                  * return the number of operations performed. For each
  137.                  * one, add the current operations to avg; they will
  138.                  * then be divided by count to get the average. Then
  139.                  * compare the min and max values to see if they should
  140.                  * be updated
  141.                  */
  142.                  
  143.                  update_result(&t.bubble, bubble(arr, i));
  144.                  update_result(&t.insertion, insertion(arr, i));
  145.                  update_result(&t.selection, selection(arr, i));
  146.             }
  147.         }
  148.        
  149.         printf("Trial for array with %d elements complete\n", i);
  150.         // Trial for length i is over; update the files
  151.         update_file(t.bubble, i, count, bub);
  152.         update_file(t.insertion, i, count,  ins);
  153.         update_file(t.selection, i, count, sel);
  154.     }
  155. }
  156.  
  157. /**
  158.  * Print contents of an array
  159.  */
  160. void print_array(int arr[], int n)
  161. {
  162.     for (int i = 0; i < n; i++) {
  163.         printf("%i", arr[i]);
  164.         if (i < n - 1) {
  165.             printf(", ");
  166.         }
  167.     }
  168. }
  169.  
  170. /**
  171.  * fill an array with values in ascending order
  172.  */
  173. void fill(int arr[], int max)
  174. {
  175.    
  176.     for (int i = 0; i < max; i++) {
  177.         arr[i] = i;
  178.     }
  179. }
  180.  
  181. /**
  182.  * Copy the values of one array to another
  183.  */
  184. void copy(int source[], int dest[], int len)
  185. {
  186.     for(int i = 0; i < len; i++) {
  187.         dest[i] = source[i];
  188.     }
  189. }
  190.  
  191. /**
  192.  * Swap two elements in an array
  193.  */
  194. void swap(int arr[], int i, int j)
  195. {
  196.     int tmp = arr[i];
  197.     arr[i] = arr[j];
  198.     arr[j] = tmp;
  199. }
  200.  
  201. /**
  202.  * Validate that an array is sorted correctly
  203.  */
  204. void validate(int arr[], int n, char *name)
  205. {
  206.     for(int i = 1; i < n; i++) {
  207.         if (arr[i] < arr[i - 1]){
  208.             printf("Error: problem with the %s algorithm. Result:\n",
  209.             name);
  210.             print_array(arr, n);
  211.             printf("\nExiting\n");
  212.             exit(1);
  213.         }
  214.     }
  215. }
  216.  
  217. /**
  218.  * Set the length of the array used in a trial, along with
  219.  * the starting values of each algorithm's results
  220.  */
  221. void prep_trial(struct trial *t, int i)
  222. {
  223.     t->length = i;
  224.     prep_result(&t->bubble);
  225.     prep_result(&t->insertion);
  226.     prep_result(&t->selection);
  227. }
  228.  
  229. /**
  230.  * Set the starting values of an algorithm's results to zero
  231.  */
  232. void prep_result(struct result *r)
  233. {
  234.     r->min = 0;
  235.     r->max = 0;
  236.     r->total = 0;
  237. }
  238.  
  239. /**
  240.  * Once a permutation of an array is sorted by an algorithm, update
  241.  * the results where applicable
  242.  */
  243. void update_result(struct result *r, int operations)
  244. {
  245.     r->total += operations;
  246.     if (r->min == 0
  247.     || operations < r->min) {
  248.         r->min = operations;
  249.     }
  250.    
  251.     if (operations > r->max) {
  252.         r->max = operations;
  253.     }
  254. }
  255.  
  256. /**
  257.  * Write the results of a trial to the appropriate csv file
  258.  */
  259. void update_file(struct result r, int len, int count, FILE *fp)
  260. {
  261.     fprintf(fp, "%d,%d,%llu, %d\n",
  262.     len,
  263.     r.max,
  264.     (r.total / count),
  265.     r.min);
  266. }
  267.  
  268. /**
  269.  * The bubble sort algorthim; returns the number of operations
  270.  * performed
  271.  */
  272. int bubble(int a[], int n)
  273. {
  274.     int swapped, operations = 0;
  275.     int arr[n];
  276.    
  277.     // Ensures that the original array does not get altered
  278.     copy(a, arr, n);
  279.    
  280.     // During each pass through the array, continue swapping pairs
  281.     // of consecutive elements in the array if the one to the right
  282.     // is less than that to the left. If no items get swapped during
  283.     // a particular pass, terminate the loop.
  284.     do {
  285.         // Flag variable to check and see if anything has been
  286.         // swapped; if not, we can stop because the array is sorted
  287.         swapped = 0;
  288.        
  289.         for (int i = 1; i < n; i++) {
  290.             if(arr[i] < arr[i - 1]) {
  291.                 // arr[i] is too big; swap it out
  292.                 swap(arr, i, i - 1);
  293.                 swapped = 1;
  294.                 operations ++;
  295.             }
  296.             operations ++;
  297.         }
  298.        
  299.         n--;
  300.     }
  301.     while (swapped);
  302.    
  303.     validate(arr, n, "bubble");
  304.    
  305.     return operations;
  306. }
  307.  
  308. /**
  309.  * The insertion sort algorthim; returns the number of operations
  310.  * performed
  311.  */
  312. int insertion(int a[], int n)
  313. {
  314.     int operations = 0;
  315.     int arr[n];
  316.    
  317.     // Ensures that the original array does not get altered
  318.     copy(a, arr, n);
  319.    
  320.     // For each item in the unsorted portion of the array,
  321.     // pull it out, find its place in the sorted portion, and
  322.     // shift the other items across accordingly, then place the
  323.     // newly sorted item in the empty space created
  324.     for(int i = 1; i < n; i++) {
  325.         int j = i, element = arr[i];
  326.        
  327.         while(j > 0 && arr[j - 1] > element) {
  328.             arr[j] = arr[j - 1];
  329.             j--;
  330.             operations ++;
  331.         }
  332.        
  333.         arr[j] = element;
  334.         operations++;
  335.     }
  336.    
  337.     validate(arr, n, "insertion");
  338.    
  339.     return operations;
  340. }
  341.  
  342. /**
  343.  * The selection sort algorthim; returns the number of operations
  344.  * performed
  345.  */
  346. int selection(int a[], int n)
  347. {
  348.     int operations = 0;
  349.     int arr[n];
  350.    
  351.     // Ensures that the original array does not get altered
  352.     copy(a, arr, n);
  353.    
  354.     // During each pass through the unsorted portion of the array,
  355.     // find the minimum value and append it to the sorted portion
  356.     for(int i = 0; i < n - 1; i++) {
  357.         int min = i;
  358.         for (int j = i + 1; j < n; j++) {
  359.             if (arr[j] < arr[min]) {
  360.                 min = j;
  361.             }
  362.             operations ++;
  363.         }
  364.        
  365.         if (min != i) {
  366.             swap(arr, min, i);
  367.             operations ++;
  368.         }
  369.     }
  370.    
  371.     validate(arr, n, "selection");
  372.    
  373.     return operations;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement