threaded mergesort

0

Możecie powiedzieć, co robię źle? Napisałem mergesorta i działał dobrze, po czym zakomentowałem wywołania rekurencyjne i dodaję do boost::thread, ale wtedy wychodzą kwiatki... :(
A jak próbuję dorobić do tego szablon i zamiast <int> dać <T> to już w ogóle Meksyk na stadionie:
193 note: candidate expects 1 argument, 4 provided
14 note: candidate expects 10 arguments, 4 provided
To tylko 2 z wielu błędów. :P

Z góry dzięki.

#include <iostream>
#include <vector>
#include <boost/thread.hpp>

void merge_sort(std::vector <int> & tab, size_t beg, size_t end)
{
    if(beg < end)
    {
        size_t pivot = (beg + end) >> 1;

        boost::thread left(merge_sort, tab, beg, pivot);
        //merge_sort(tab, beg, pivot);
        boost::thread right(merge_sort, tab, pivot + 1, end);
        //merge_sort(tab, pivot + 1, end);
        left.join();
        right.join();

        std::vector <int> buf (tab);
        size_t i = beg, j = pivot + 1, q = beg;
        while (i <= pivot && j <= end)
        {
            if (buf[i] < buf[j])
                tab[q++] = buf[i++];
            else
                tab[q++] = buf[j++];
        }
        while (i <= pivot)
            tab[q++] = buf[i++];
    }
}

int main()
{

    const int myints[] = {30,29,28,27,26,25,1,2,3,4,5,6,7,24,23,22,21,20,19,18,8,9,10,11,17,16,15,13,45,12};
    std::vector <int> kontener (myints, myints + sizeof(myints) / sizeof(int) );

    merge_sort(kontener, 0, kontener.size() - 1);

    for(std::vector <int>::iterator it = kontener.begin(); it != kontener.end(); it++)
        std::cout << *it << " ";
    std::cout << std::endl;

    std::cin.sync();
    std::cin.get();
    return(0);
}
0

Niby jakim cudem miał działać dobrze jeżeli:

  1. masz za mało pętli brakuje while(j <= end)
  2. masz pomieszanie z popłataniem w tych +1, <, <= to się nie dobiera metodą prób i błędów.
  3. obejrzyj sobie jeszcze raz konstruktor dla boost::thread

Powinieneś to zrobić na wzorzec standardowej funkcji sort:

#include <iostream>
#include <vector>
 
template<class Iterator> void merge_sort(Iterator first,Iterator last)
  {
   size_t size=last-first;
   if(size>1)
     {
      Iterator pivot=first+(size>>1),mid=pivot,start=first;
      merge_sort(first,pivot);
      merge_sort(pivot,last);
      std::vector<int> buf(size);
      std::vector<int>::iterator wrt=buf.begin();
      while((first<mid)&&(pivot<last)) *(wrt++)=*first<*pivot?*(first++):*(pivot++);
      while(first<mid) *(wrt++)=*(first++);
      while(pivot<last) *(wrt++)=*(pivot++);
      wrt=buf.begin();
      while(wrt!=buf.end()) *(start++)=*(wrt++);
     }
  }
 
int main()
  {
   int myints[]={30,29,28,27,26,25,1,2,3,4,5,6,7,24,23,22,21,20,19,18,8,9,10,11,17,16,15,13,45,12};
   const unsigned mysize=sizeof(myints)/sizeof(*myints);
   std::vector<int> kontener(myints,myints+mysize);
 
   merge_sort(kontener.begin(),kontener.end());
   merge_sort(myints,myints+mysize);
 
   for(const int *it=myints;it!=myints+mysize;++it) std::cout<<*it<<" ";
   std::cout<<std::endl;
   for(std::vector<int>::iterator it=kontener.begin();it!=kontener.end();++it) std::cout<<*it<<" ";
   std::cout<<std::endl;
 
   std::cin.sync();
   std::cin.get();
   return(0);
  }
0
_13th_Dragon napisał(a):

Niby jakim cudem miał działać dobrze jeżeli:



No zakomentuj konstruktory thread i joiny i odkomentuj wywołania rekurencyjne to zobaczysz.

1 użytkowników online, w tym zalogowanych: 0, gości: 1