Guest User

Untitled

a guest
Sep 23rd, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <climits>
  5. #include <cmath>
  6.  
  7.  
  8. typedef struct Tdata {
  9.     unsigned long long key;
  10.     char value[65];
  11. } T_data;
  12.  
  13.  
  14. typedef struct Tlist {
  15.     struct Tlist * next;
  16.     struct Tlist * prev;
  17.     struct Tdata data;
  18.     bool isEmpty;
  19. } T_list;
  20.  
  21.  
  22. Tlist * Create (Tlist * list) {
  23.     Tlist * newlist = new Tlist;
  24.     newlist->prev = NULL;
  25.     newlist->next = NULL;
  26.     newlist->isEmpty = true;
  27.     return newlist;
  28. }
  29.  
  30. void Append_to_list (Tdata data, Tlist * list) {
  31.     unsigned long long x = data.key;
  32.     bool isFirst = false, isLast = false;
  33.     if (list->isEmpty) {
  34.         list->data = data;
  35.         list->isEmpty = false;
  36.         return;
  37.     }
  38.     else {
  39.         Tlist * newlist = new Tlist;
  40.         Tlist * temp = new Tlist;
  41.         newlist->data = data;
  42.         if (x < list->data.key) {
  43.             while (x < list->data.key) {
  44.                 temp = list;
  45.                 list = list->prev;
  46.                 if (list == NULL) {
  47.                     isFirst = true;
  48.                     break;
  49.                 }
  50.             }
  51.             if (isFirst) {
  52.                 newlist->prev = NULL;
  53.                 newlist->next = temp;
  54.                 temp->prev = newlist;
  55.             }
  56.             else {
  57.                 newlist->prev = list;
  58.                 newlist->next = list->next;
  59.                 list->next->prev = newlist;
  60.                 list->next = newlist;
  61.             }
  62.             return;
  63.  
  64.         }
  65.         else if (x >= list->data.key) {
  66.             while (x >= list->data.key) {
  67.                 temp = list;
  68.                 list = list->next;
  69.                 if (list == NULL) {
  70.                     isLast = true;
  71.                     break;
  72.                 }
  73.             }
  74.             if (isLast) {
  75.                 newlist->next = NULL;
  76.                 newlist->prev = temp;
  77.                 temp->next = newlist;
  78.             }
  79.             else {
  80.                 newlist->next = list;
  81.                 newlist->prev = list->prev;
  82.                 list->prev->next = newlist;
  83.                 list->prev = newlist;
  84.             }
  85.             return;
  86.         }
  87.     }
  88. }
  89.  
  90. Tdata * Bucket_Sort (Tdata *array, Tlist ** list_array, int array_size, unsigned long long const_frag, int number_of_lists) {
  91.     int * list_size = new int[array_size];
  92.     for (int i = 0; i < number_of_lists; i++) { // or array_size
  93.         list_array[i] = Create(list_array[i]);
  94.         list_size[i] = 0;
  95.     }
  96.     for (int i = 0; i < array_size; i++) {
  97.         int list_num = 0;
  98.         unsigned long long frag = const_frag;
  99.         while (1) {
  100.             if (array[i].key <= frag) {
  101.                 Append_to_list(array[i], list_array[list_num]);  // adding Tdata struct
  102.                 list_size[i]++;
  103.                 break;
  104.             }
  105.             else {
  106.                 frag += const_frag;
  107.                 list_num++;
  108.                 if (list_num +1 ==  number_of_lists) { //array_size
  109.                     frag = ULLONG_MAX;
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     int list_num = 0;
  115.     int i = 0;
  116.  
  117.     for (int j = 0; j < number_of_lists; j++) { // array_size
  118.         if (list_array[list_num]->isEmpty == false) {
  119.             while (list_array[list_num]->prev != NULL) {
  120.                 list_array[list_num] = list_array[list_num]->prev;
  121.             }
  122.             while (list_array[list_num] != NULL) {
  123.                 array[i] = list_array[list_num]->data;
  124.                 i++;
  125.                 list_array[list_num] = list_array[list_num]->next;
  126.             }
  127.         }
  128.         list_num++;
  129.     }
  130.     return array;
  131. }
  132.  
  133.  
  134. int main() {
  135.  
  136.     Tdata * array = (Tdata*) std::malloc(10 * sizeof(Tdata));
  137.     int array_size = 0;
  138.     int current_size = 10;
  139.  
  140.     unsigned long long key;
  141.     char value[65];
  142.  
  143.     while (std::cin >> key >> value) {
  144.         array[array_size].key = key;
  145.        // std::cout << value << std::endl;
  146.         strcpy(array[array_size].value, value);
  147.         array_size++;
  148.         if (array_size+1 == current_size) {
  149.             int new_size = array_size * 2;
  150.             current_size = new_size;
  151.             array = (Tdata *) std::realloc(array, new_size * sizeof(Tdata));
  152.         }
  153.     }
  154.  
  155.     int number_of_lists = array_size;
  156.     unsigned long long frag;
  157.     if (array_size != 0) {
  158.         frag = ULLONG_MAX /number_of_lists; // or / array_size
  159.     }
  160.     else {
  161.         return 0;
  162.     }
  163.     // sort
  164.  
  165.     Tlist ** list_array = new Tlist * [number_of_lists];
  166.     unsigned int start_time =  clock();
  167.     array = Bucket_Sort(array, list_array, array_size, frag, number_of_lists);
  168.     unsigned int end_time = clock(); // конечное время
  169.     unsigned int search_time = end_time - start_time; // искомое время
  170.     for (int i = 0; i < array_size; i++) {
  171.         std::cout << array[i].key << "\t" << array[i].value << std::endl;
  172.     }
  173.     std::cout << search_time <<std::endl;
  174.     delete array;
  175.     delete list_array;
  176.  
  177.     return 0;
  178. }
Add Comment
Please, Sign In to add comment