home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / SORTTHD.CPP < prev    next >
C/C++ Source or Header  |  1997-02-14  |  5KB  |  162 lines

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