Advertisement
Guest User

RadixSort.c

a guest
May 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <limits.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <string.h>
  7.  
  8. //MacOS timespec
  9.  
  10. int numbers[800];
  11.  
  12. // Get size of statically allocated array
  13. #define ARR_LEN(ARR) (sizeof ARR / sizeof *ARR)
  14. // Generate random number in the interval [M,N]
  15.  
  16. static void swap(unsigned *a, unsigned *b) {
  17.     unsigned tmp = *a;
  18.     *a = *b;
  19.     *b = tmp;
  20. }
  21.  
  22. /* sort unsigned ints */
  23. static void rad_sort_u(unsigned *from, unsigned *to, unsigned bit)
  24. {
  25.     if (!bit || to < from + 1) return;
  26.  
  27.     unsigned *ll = from, *rr = to - 1;
  28.     for (;;) {
  29.         /* find left most with bit, and right most without bit, swap */
  30.         while (ll < rr && !(*ll & bit)) ll++;
  31.         while (ll < rr &&  (*rr & bit)) rr--;
  32.         if (ll >= rr) break;
  33.         swap(ll, rr);
  34.     }
  35.  
  36.     if (!(bit & *ll) && ll < to) ll++;
  37.     bit >>= 1;
  38.  
  39.     rad_sort_u(from, ll, bit);
  40.     rad_sort_u(ll, to, bit);
  41. }
  42.  
  43. /* sort signed ints: flip highest bit, sort as unsigned, flip back */
  44. static void radix_sort(int *a, const size_t len)
  45. {
  46.     size_t i;
  47.     unsigned *x = (unsigned*) a;
  48.  
  49.     for (i = 0; i < len; i++)
  50.             x[i] ^= INT_MIN;
  51.  
  52.         rad_sort_u(x, x + len, INT_MIN);
  53.  
  54.         for (i = 0; i < len; i++)
  55.             x[i] ^= INT_MIN;
  56. }
  57.  
  58. void get_numbers(char * arr) {
  59.         FILE *file;
  60.             file = fopen(arr, "r");
  61.         size_t read;
  62.         char * line = NULL;
  63.         size_t line_len = 0;
  64.  
  65.         size_t buffer_size = 10;
  66.         int * buffer = (int *)malloc(sizeof(int) * buffer_size);
  67.  
  68.         int seek = 0;
  69.         while((read = getline(&line, &line_len, file)) != -1) {
  70.             buffer[seek++] = atoi(line);
  71.  
  72.             if (seek % 10 == 0) {
  73.                 buffer_size += 10;
  74.                 buffer = (int *)realloc(buffer, sizeof(int) * buffer_size);
  75.             }
  76.  
  77.         }
  78.  
  79.         for (int i = 0; i < seek; i++) {
  80.                     numbers[i] = buffer[i];
  81.             //printf("%d\n", numbers[i]);
  82.         }
  83.             //printf("%lu\n", ARR_LEN(numbers));
  84.         free(buffer);
  85.         fclose(file);
  86.     }
  87.  
  88.   // writes the result to the output file
  89.     void file_write(int _time, int loopN, char * str) {
  90.         FILE *fp;
  91.     fp = fopen(str, "a");
  92.         //fp = NULL;
  93.     fprintf(fp, "%d,%d\n", loopN, _time);
  94.     fclose(fp);
  95.     }
  96.  
  97.   // returns time to sort the list once in nanoseconds
  98.     int _sort(int * num) {
  99.  
  100.         memset(numbers, 0, sizeof(numbers));
  101.  
  102.         struct timespec start, end;
  103.         clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  104.         radix_sort(num, ARR_LEN(num));
  105.         clockgettime(CLOCK_MONOTONIC_RAW, &end);
  106.         return (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
  107.     }
  108.  
  109. int main(int argc, char *argv[])
  110. {
  111.     get_numbers(argv[1]);
  112.     int * num = numbers;
  113.  
  114.     // data file:   argv[1]);
  115.     // output file: argv[2]);
  116.     // loops n:         argv[3]);
  117.     for(int i = 1; i <= (int) strtol(argv[3], (char **)NULL, 10); i++) {
  118.         file_write(_sort(num), i, argv[2]);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement