|
| 1 | +/****************************************************************************** |
| 2 | + * LICENSE * |
| 3 | + ****************************************************************************** |
| 4 | + * This file is free software: you can redistribute it and/or modify it * |
| 5 | + * it under the terms of the GNU General Public License as published by * |
| 6 | + * the Free Software Foundation, either version 3 of the License, or * |
| 7 | + * (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | + * GNU General Public License for more details. * |
| 13 | + ******************************************************************************/ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <math.h> |
| 18 | + |
| 19 | +/* Global variable for counting the number of pictures drawn. */ |
| 20 | +unsigned int current_pic = 0; |
| 21 | + |
| 22 | +/* Function for drawing a scatter plot of an array arr of length arr_length. */ |
| 23 | +void draw_pic(double *arr, unsigned int arr_length) |
| 24 | +{ |
| 25 | + /* Declare variables for sampling the box we're drawing to. */ |
| 26 | + unsigned int x, y; |
| 27 | + double z_x, z_y; |
| 28 | + double rcpr_factor = 1.0 / (double)arr_length; |
| 29 | + |
| 30 | + /* Set the boundaries of the box. */ |
| 31 | + double y_min = 0.0; |
| 32 | + double y_max = 1.0; |
| 33 | + double pixel_width = 0.001; |
| 34 | + |
| 35 | + /* Variables for coloring the current pixel. */ |
| 36 | + unsigned char black = 0U; |
| 37 | + unsigned char white = 255U; |
| 38 | + |
| 39 | + /* And a variable for writing to a file. */ |
| 40 | + FILE *fp; |
| 41 | + |
| 42 | + /* Create the filename. And array of char's acts as a string in C. */ |
| 43 | + char str[80]; |
| 44 | + |
| 45 | + /* Use the sprintf function in stdio.h to create a formatted string str * |
| 46 | + * which will serve as the name of the file we're drawing. We're * |
| 47 | + * formatting it by appending the picture counter at the end to prevent * |
| 48 | + * overwriting previous drawings. We'll increment this counter at the * |
| 49 | + * end of our drawing procedure. */ |
| 50 | + sprintf(str, "merge_sort_%u.pgm", current_pic); |
| 51 | + |
| 52 | + /* Open the PGM file and write the preamble to it. */ |
| 53 | + fp = fopen(str, "w"); |
| 54 | + fprintf(fp, "P5\n%d %d\n255\n", arr_length, arr_length); |
| 55 | + |
| 56 | + /* Loop through each pixel. */ |
| 57 | + for (y=0; y<arr_length; ++y) |
| 58 | + { |
| 59 | + /* We want to center z_y so scale and shift. This makes the output * |
| 60 | + * picture lie in the box [x_min, x_max] x [y_min, y_max]. */ |
| 61 | + z_y = y * (y_max - y_min) * rcpr_factor + y_min; |
| 62 | + z_y = y_max - z_y; |
| 63 | + |
| 64 | + /* Loop over every x pixel as well. */ |
| 65 | + for (x=0; x<arr_length; ++x) |
| 66 | + { |
| 67 | + /* No need to center x, just set it as a double. */ |
| 68 | + z_x = (double)x; |
| 69 | + |
| 70 | + /* If arr[x] and z_y are close, color the pixel white meaning * |
| 71 | + * we have a point in the plane corresponding to the data. */ |
| 72 | + if (fabs(arr[x] - z_y) < pixel_width) |
| 73 | + fputc(white, fp); |
| 74 | + |
| 75 | + /* Otherwise, color the background black. */ |
| 76 | + else |
| 77 | + fputc(black, fp); |
| 78 | + } |
| 79 | + /* End of for-loop for the pixels x-coordinate. */ |
| 80 | + } |
| 81 | + /* End of for-loop for the pixels y-coordinate. */ |
| 82 | + fclose(fp); |
| 83 | + |
| 84 | + /* Increment the picture counter. */ |
| 85 | + ++current_pic; |
| 86 | +} |
| 87 | +/* End of draw_pic. */ |
| 88 | + |
| 89 | +/* Function for merging an unsorted array using a sorted reference array. * |
| 90 | + * This is similar to recursive merge sort, but uses an iterative scheme. */ |
| 91 | +void merge(double *unsorted, unsigned left, unsigned int right, |
| 92 | + unsigned int end, double *sorted) |
| 93 | +{ |
| 94 | + /* Declare variables for indexing. */ |
| 95 | + unsigned int m, n, k; |
| 96 | + |
| 97 | + /* Like the recursive method, we're going to go left to right and set * |
| 98 | + * the current value of the array we're sorting to the lesser of the two * |
| 99 | + * values we're comparing. */ |
| 100 | + m = left; |
| 101 | + n = right; |
| 102 | + |
| 103 | + /* Loop over all of the values in the current window we're merging. */ |
| 104 | + for (k = left; k < end; ++k) |
| 105 | + { |
| 106 | + if ((m < right) && ((n >= end) || (unsorted[m] <= unsorted[n]))) |
| 107 | + { |
| 108 | + sorted[k] = unsorted[m]; |
| 109 | + ++m; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + sorted[k] = unsorted[n]; |
| 114 | + ++n; |
| 115 | + } |
| 116 | + } |
| 117 | + /* End of for-loop merging the sorted data. */ |
| 118 | +} |
| 119 | +/* End of merge. */ |
| 120 | + |
| 121 | +/* Iterative merge-sort algorithm for an array arr of size arr_size. */ |
| 122 | +double *merge_sort(double *arr, unsigned int arr_size) |
| 123 | +{ |
| 124 | + /* Declare all necessary variables. */ |
| 125 | + unsigned int width; |
| 126 | + unsigned int left, right, end, n; |
| 127 | + double *unsorted, *sorted; |
| 128 | + |
| 129 | + /* We're going to copy the data in arr into unsorted so as not to * |
| 130 | + * potentially corrupt or change the input. */ |
| 131 | + unsorted = malloc(sizeof(*unsorted) * arr_size); |
| 132 | + sorted = malloc(sizeof(*sorted) * arr_size); |
| 133 | + |
| 134 | + /* Set both sorted and unsorted to the same values as arr. */ |
| 135 | + for (n=0; n<arr_size; ++n) |
| 136 | + { |
| 137 | + unsorted[n] = arr[n]; |
| 138 | + sorted[n] = arr[n]; |
| 139 | + } |
| 140 | + |
| 141 | + /* Draw the starting values of the unsorted data. */ |
| 142 | + draw_pic(sorted, arr_size); |
| 143 | + |
| 144 | + /* We're splitting the array into smaller powers of two. This iterative * |
| 145 | + * scheme is equivalent to the recursive one. */ |
| 146 | + for (width = 1; width < arr_size; width = 2 * width) |
| 147 | + { |
| 148 | + for (left = 0; left < arr_size; left = left + 2 * width) |
| 149 | + { |
| 150 | + /* Ensure that right hasn't gone beyond the size of the array. * |
| 151 | + * This can happen if the array size isn't a power of two. */ |
| 152 | + if ((left + width) < arr_size) |
| 153 | + right = left + width; |
| 154 | + else |
| 155 | + right = arr_size; |
| 156 | + |
| 157 | + /* Similarly, ensure that end isn't beyond the array size. */ |
| 158 | + if (left + 2*width < arr_size) |
| 159 | + end = left + 2*width; |
| 160 | + else |
| 161 | + end = arr_size; |
| 162 | + |
| 163 | + /* Sort the current window we're looking at. */ |
| 164 | + merge(sorted, left, right, end, unsorted); |
| 165 | + } |
| 166 | + |
| 167 | + /* Merge sorted the sorted data into unsorted. Copy this into sorted.*/ |
| 168 | + for (n = 0; n < arr_size; ++n) |
| 169 | + sorted[n] = unsorted[n]; |
| 170 | + |
| 171 | + /* Draw what the data currently looks like. */ |
| 172 | + draw_pic(sorted, arr_size); |
| 173 | + } |
| 174 | + |
| 175 | + /* Free the malloc'd pointer unsorted and return sorted. */ |
| 176 | + free(unsorted); |
| 177 | + return sorted; |
| 178 | +} |
| 179 | +/* End of merge_sort. */ |
| 180 | + |
| 181 | +/* Function for returning a random real number in [0, 1]. */ |
| 182 | +double real_rand(void) |
| 183 | +{ |
| 184 | + return (double) rand()/RAND_MAX; |
| 185 | +} |
| 186 | + |
| 187 | +/* Function for testing our sorting algorithm. */ |
| 188 | +int main(void) |
| 189 | +{ |
| 190 | + /* Declare a variable for indexing. */ |
| 191 | + unsigned int n; |
| 192 | + |
| 193 | + /* And the size of the random array we're making. A power of 2 makes the * |
| 194 | + * algorithm faster, so use this. */ |
| 195 | + unsigned int N = 4*1024U; |
| 196 | + double *sorted; |
| 197 | + |
| 198 | + /* Allocate memory for the random unsorted array. */ |
| 199 | + double *x = malloc(sizeof(*x) * N); |
| 200 | + |
| 201 | + /* Set the entries of x to random values. */ |
| 202 | + for (n = 0; n < N; ++n) |
| 203 | + x[n] = real_rand(); |
| 204 | + |
| 205 | + /* Sort the data. */ |
| 206 | + sorted = merge_sort(x, N); |
| 207 | + |
| 208 | + /* Check if our sort work. If it did, nothing should print. */ |
| 209 | + for (n = 1; n < N; ++n) |
| 210 | + { |
| 211 | + if (sorted[n] < sorted[n-1]) |
| 212 | + { |
| 213 | + puts("Not Sorted!"); |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + free(sorted); |
| 218 | + return 0; |
| 219 | +} |
| 220 | +/* End of main. */ |
0 commit comments