home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / SORTTHD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-05  |  4.4 KB  |  158 lines

  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "sortthd.h"
  6. //---------------------------------------------------------------------
  7. void __fastcall PaintLine(TCanvas *Canvas, int I, int Len)
  8. {
  9.   TPoint points[2];
  10.  
  11.   points[0] = Point(0, I*2+1);
  12.   points[1] = Point(Len, I*2+1);
  13.  
  14.   Canvas->Polyline(EXISTINGARRAY(points));
  15. }
  16. //---------------------------------------------------------------------
  17. __fastcall TSortThread::TSortThread(TPaintBox *Box, Integer *SortArray, 
  18.     const Integer SortArray_Size)
  19.     : TThread(False)
  20. {
  21.   FBox = Box;
  22.   FSortArray = SortArray;
  23.   FSize = SortArray_Size + 1;
  24.   FreeOnTerminate = True;
  25. }
  26. //---------------------------------------------------------------------
  27. /* Since DoVisualSwap uses a VCL component (i.e., the TPaintBox) it should never
  28.   be called directly by this thread.  DoVisualSwap should be called by passing
  29.   it to the Synchronize method which causes DoVisualSwap to be executed by the
  30.   main VCL thread, avoiding multi-thread conflicts. See VisualSwap for an
  31.   example of calling Synchronize. */
  32.  
  33. void __fastcall TSortThread::DoVisualSwap()
  34. {
  35.   TCanvas *canvas;
  36.   canvas = FBox->Canvas;
  37.   canvas->Pen->Color = clBtnFace;
  38.   PaintLine(canvas, FI, FA);
  39.   PaintLine(canvas, FJ, FB);
  40.   canvas->Pen->Color = clRed;
  41.   PaintLine(canvas, FI, FB);
  42.   PaintLine(canvas, FJ, FA);
  43. }
  44. //---------------------------------------------------------------------
  45. /* VisusalSwap is a wrapper on DoVisualSwap making it easier to use.  The
  46.   parameters are copied to instance variables so they are accessable
  47.   by the main VCL thread when it executes DoVisualSwap */
  48.  
  49. void __fastcall TSortThread::VisualSwap(int A, int B, int I, int J)
  50. {
  51.   FA = A;
  52.   FB = B;
  53.   FI = I;
  54.   FJ = J;
  55.  
  56.   Synchronize(DoVisualSwap);
  57. }
  58. //---------------------------------------------------------------------
  59. /* The Execute method is called when the thread starts */
  60.  
  61. void __fastcall TSortThread::Execute()
  62. {
  63.   Sort(FSortArray, FSize-1);
  64. }
  65. //---------------------------------------------------------------------
  66. __fastcall TBubbleSort::TBubbleSort(TPaintBox *Box, Integer *SortArray, 
  67.   const Integer SortArray_Size)
  68.   : TSortThread(Box, SortArray, SortArray_Size)
  69. {
  70. }
  71. //---------------------------------------------------------------------
  72. void __fastcall TBubbleSort::Sort(int *A, int const AHigh)
  73. {
  74.   int I, J, T;
  75.  
  76.   for (I=AHigh; I >= 0; I--)
  77.     for (J=0; J<=AHigh-1; J++)
  78.       if (A[J] > A[J + 1])
  79.       {
  80.         VisualSwap(A[J], A[J + 1], J, J + 1);
  81.         T = A[J];
  82.         A[J] = A[J + 1];
  83.         A[J + 1] = T;
  84.         if (Terminated)
  85.           return;
  86.       }
  87. }
  88. //---------------------------------------------------------------------
  89. __fastcall TSelectionSort::TSelectionSort(TPaintBox *Box, Integer *SortArray, 
  90.   const Integer SortArray_Size)
  91.   : TSortThread(Box, SortArray, SortArray_Size)
  92. {
  93. }
  94. //---------------------------------------------------------------------
  95. void __fastcall TSelectionSort::Sort(int *A, int const AHigh)
  96. {
  97.   int I, J, T;
  98.  
  99.   for (I=0; I <= AHigh-1; I++)
  100.     for (J=AHigh; J >= I+1; J--)
  101.       if (A[I] > A[J])
  102.       {
  103.         VisualSwap(A[I], A[J], I, J);
  104.         T = A[I];
  105.         A[I] = A[J];
  106.         A[J] = T;
  107.         if (Terminated) 
  108.           return;
  109.       }
  110. }
  111. //---------------------------------------------------------------------
  112. __fastcall TQuickSort::TQuickSort(TPaintBox *Box, Integer *SortArray, 
  113.   const Integer SortArray_Size)
  114.   : TSortThread(Box, SortArray, SortArray_Size)
  115. {
  116. }
  117. //---------------------------------------------------------------------
  118. void __fastcall TQuickSort::QuickSort(int *A, int const AHigh, int iLo, int iHi)
  119. {
  120.   int Lo, Hi, Mid, T;
  121.  
  122.   Lo = iLo;
  123.   Hi = iHi;
  124.   Mid = A[(Lo+Hi)/2];
  125.  
  126.   do
  127.   {
  128.     if (Terminated)
  129.       return; 
  130.     while (A[Lo] < Mid)
  131.         Lo++;
  132.     while (A[Hi] > Mid) 
  133.         Hi--;
  134.     if (Lo <= Hi)
  135.     {
  136.       VisualSwap(A[Lo], A[Hi], Lo, Hi);
  137.       T = A[Lo];
  138.       A[Lo] = A[Hi];
  139.       A[Hi] = T;
  140.       Lo++;
  141.       Hi--;
  142.     }
  143.   }
  144.   while (Lo <= Hi);
  145.  
  146.   if (Hi > iLo) 
  147.     QuickSort(A, AHigh, iLo, Hi);
  148.   if (Lo < iHi) 
  149.     QuickSort(A, AHigh, Lo, iHi);
  150. }
  151. //---------------------------------------------------------------------
  152. void __fastcall TQuickSort::Sort(int *A, int const AHigh)
  153. {
  154.   QuickSort(A, AHigh, 0, AHigh);
  155. }
  156. //---------------------------------------------------------------------
  157.    
  158.