Advertisement
NickG

test prog

May 10th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. #ifdef WIN32
  8. #include <Windows.h>
  9. #else
  10. #include <sys/time.h>
  11. #include <ctime>
  12. #endif
  13.  
  14. /* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
  15.  * windows and linux. */
  16.  
  17. int GetTimeMs64()
  18. {
  19. #ifdef WIN32
  20.  /* Windows */
  21.  FILETIME ft;
  22.  LARGE_INTEGER li;
  23.  
  24.  /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  25.   * to a LARGE_INTEGER structure. */
  26.  GetSystemTimeAsFileTime(&ft);
  27.  li.LowPart = ft.dwLowDateTime;
  28.  li.HighPart = ft.dwHighDateTime;
  29.  
  30.  uint64 ret = li.QuadPart;
  31.  ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
  32.  ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
  33.  
  34.  return ret;
  35. #else
  36.  /* Linux */
  37.  struct timeval tv;
  38.  
  39.  gettimeofday(&tv, NULL);
  40.  
  41.  int ret = tv.tv_usec;
  42.  /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
  43.  ret /= 1000;
  44.  
  45.  /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
  46.  ret += (tv.tv_sec * 1000);
  47.  
  48.  return ret;
  49. #endif
  50. }
  51.  
  52.  
  53.  
  54. void radixsort(int array[], int size) {
  55.  
  56. //Set max equal to the greatest element of the array
  57. int max = 0;
  58. for (int i = 0; i < size; i++) {
  59.    if (array[i] > max)
  60.       max = array[i];
  61.    }
  62.  
  63. for(int pow10=1; max != 0; max/=10, pow10*=10) {
  64.    int buffer[size], bucket[10] = {0};
  65.  
  66.  
  67.       //Calculate the bucket numbers
  68.       for (int i = 0; i < size; i++)
  69.          bucket[(array[i] / pow10) % 10]++;
  70.      
  71.       //Sum the bucket numbers
  72.       for (int i = 1; i<10; i++)
  73.          bucket[i] += bucket[i - 1];
  74.  
  75.       //Bucket sort the array into a buffer
  76.       for  (int i = size - 1; i >= 0; i--)
  77.          buffer[--bucket[(array[i] / pow10) % 10]] = array[i];
  78.  
  79.       //Move the sorted elements back into array
  80.       for (int i = 0; i < size; i++)
  81.          array[i] = buffer[i];
  82.   }
  83. }
  84.  
  85.  
  86. void radixsort2(int array[], int size) {
  87.  
  88. //Set max equal to the greatest element of the array
  89. int max = 0;
  90. for (int i = 0; i < size; i++) {
  91.    if (array[i] > max)
  92.       max = array[i];
  93.    }
  94.  
  95. for(int pow10=1; max != 0; max/=10, pow10*=10) {
  96.    int buffer[size], blarg[size], bucket[10] = {0};
  97.  
  98.       //This might improve speed
  99.       for (int i = 0; i < size; i++)
  100.          blarg[i]=(array[i] / pow10) % 10;
  101.  
  102.       //Calculate the bucket numbers
  103.       for (int i = 0; i < size; i++)
  104.          bucket[blarg[i]]++;
  105.      
  106.       //Sum the bucket numbers
  107.       for (int i = 1; i<10; i++)
  108.          bucket[i] += bucket[i - 1];
  109.  
  110.       //Bucket sort the array into a buffer
  111.       for  (int i = size - 1; i >= 0; i--)
  112.          buffer[--bucket[blarg[i]]] = array[i];
  113.  
  114.       //Move the sorted elements back into array
  115.       for (int i = 0; i < size; i++)
  116.          array[i] = buffer[i];
  117.   }
  118. }
  119.  
  120. int main() {
  121. const int size =100000;
  122. int array[size];
  123. int start, end;
  124.  
  125. srand ( time(NULL) );
  126.  
  127. start = GetTimeMs64();
  128.  
  129. for (int i=0; i<size; i++)
  130. array[i]=rand();
  131.  
  132. radixsort(array, size);
  133. end = GetTimeMs64();
  134. cout << "radixsort took " << end-start << "ms" <<endl;
  135.  
  136. for (int i=0; i<size; i++)
  137. array[i]=rand();
  138.  
  139. start = GetTimeMs64();
  140. radixsort2(array, size);
  141. end = GetTimeMs64();
  142. cout << "radixsort2 took " << end-start << "ms" <<endl;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement