Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include "b_sort.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. b_sort::b_sort(void)
  7. {
  8.     count = 0;
  9.     firstComp = NULL;
  10.     currentComp = NULL;
  11. }
  12.  
  13. b_sort::~b_sort(void)
  14. {
  15.     while (firstComp != NULL)
  16.     {
  17.         comparator *temp = firstComp->next;
  18.         delete firstComp;
  19.         firstComp = temp;
  20.     }
  21. }
  22.  
  23. unsigned int b_sort::join(unsigned int i0, unsigned int i1, unsigned int shift, unsigned int n0, unsigned int n1)
  24. {
  25.     unsigned int i, tactCount0, tactCount1;
  26.  
  27.     if (n0 * n1 < 1)
  28.         return 0;
  29.  
  30.     if (n0 == 1 && n1 == 1) {
  31.         push(i0, i1);
  32.         return 1;
  33.     }
  34.  
  35.     tactCount0 = join(i0, i1, shift * 2, n0 - (n0) / 2, n1 - (n1) / 2);
  36.     tactCount1 = join(i0 + shift, i1 + shift, shift * 2, (n0) / 2, (n1) / 2);
  37.     for (i = 1; i < (n0 - 1); i+=2) {
  38.         push(i0+shift*i, i0+shift*(i+1));
  39.     }
  40.  
  41.     if (n0%2==0) {
  42.         push(i0+shift*(n0-1), i1);
  43.  
  44.         i = 1;
  45.     } else {
  46.         i = 0;
  47.     }
  48.  
  49.     for (;i<n1-1;i+=2){
  50.         push(i1+shift*i, i1+shift*(i+1));
  51.     }
  52.  
  53.     return tactCount0 > tactCount1 ? tactCount0 + 1 : tactCount1 + 1;
  54. }
  55.  
  56.  
  57. unsigned int b_sort::sort(unsigned int i, unsigned int step, unsigned int n)
  58. {
  59.     unsigned int tacts0, tacts1, tacts2;
  60.  
  61.     if (n < 2)
  62.         return 0;
  63.  
  64.     if (n == 2){
  65.         push(i, i + step);
  66.         return 1;
  67.     }
  68.  
  69.     tacts0 = sort(i, step, n / 2);
  70.     tacts1 = sort(i + step * (n / 2), step, n - n / 2);
  71.     tacts2 = join(i, i + step * (n / 2), step, n / 2, n - n / 2);
  72.     if (tacts0 > tacts1) {
  73.         return tacts0 + tacts2;
  74.     } else {
  75.         return tacts1 + tacts2;
  76.     }
  77.     return 0;
  78. }
  79.  
  80.  
  81. void b_sort::push(unsigned int i0, unsigned int i1)
  82. {
  83.     if (firstComp == NULL) {
  84.         firstComp = new comparator;
  85.         firstComp->first = i0;
  86.         firstComp->second = i1;
  87.         firstComp->next = NULL;
  88.         currentComp = firstComp;
  89.     }
  90.     else {
  91.         currentComp->next = new comparator;
  92.         currentComp = currentComp->next;
  93.         currentComp->first = i0;
  94.         currentComp->second = i1;
  95.         currentComp->next = NULL;
  96.     }
  97.     count++;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement