Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jan 31st, 2012  |  syntax: C++ (with QT extensions)  |  size: 1.05 KB  |  hits: 80  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #ifndef _SORT_H
  2. #define _SORT_H
  3.  
  4. #include <algorithm>
  5. #include <QTimer>
  6. #include <QtGlobal>
  7. #include <QThread>
  8. #include <QDebug>
  9.  
  10.  
  11. template <typename T>class _Sort
  12. {
  13.     static void sort(T* Buff, int l, int r)
  14.     {
  15.         int i=l;
  16.         int j=r;
  17.         int x=Buff[(l+r)/2];
  18.         do
  19.         {
  20.             while(Buff[i]<x) ++i;
  21.             while(Buff[j]>x) --j;
  22.             if(i<=j)
  23.             {
  24.                 std::swap(Buff[i],Buff[j]);
  25.                 i++; j--;
  26.             }
  27.          } while(i<=j);
  28.          if(l<j) sort(Buff,l,j);
  29.          if(i<r) sort(Buff,i,r);
  30.     }
  31.  
  32.  
  33. class MyThread : public QThread
  34. {
  35.     Q_OBJECT
  36.  
  37. T* Buff;
  38. quint32 Size;
  39. public:
  40.     MyThread(){}
  41.     MyThread(T* B,quint32 S) : Buff(B),Size(S){}
  42.     void run()
  43.     {
  44.         sort(Buff,0,Size-1);
  45.         exec();
  46.     }
  47. };
  48.  
  49.  
  50. public:
  51.     static bool qSort(T* Buff, size_t Size, uint ThreadCount)
  52.     {
  53.         if (Buff == NULL || Size <= 1 || ThreadCount%2 != 0 || ThreadCount < 2)
  54.         {
  55.             qDebug() << "Error in params";
  56.             return false;
  57.         }
  58.         MyThread Thread(Buff,Size);
  59.         Thread.start();
  60.         return true;
  61.     }
  62. };
  63.  
  64. #endif // _SORT_H