Using A Sort Animation Project

First create a new C++ project. Select the type "Sort Animation Template", give the project a unique name, and click OK. Click OK on the information window, and you're set.

You must modify three member functions of the CxxxCtrl class, where xxx is your project name. These are the constructor, CxxxCtrl( ), the DoPropExchange function, and the Sort function.

CxxxCtrl( )

Set the parameters of the L.SetBarCount, L.SetTempCount, and L.SetTextCount functions to set the default number of Bars, Temps, and Text Lines. Set the value of m_interval to set the default delay period that is used when synchronizing the display. m_interval is in milliseconds, and the default is 500 (1/2 second). 500 is usually too slow.

DoPropExchange

Change the third parameter of the PX_Long functions to set the default number of bars and delay (m_interval). It is necessary to change these default values in both functions for them to work in all environments. In this function, you can also change default bar width, minimum bar height, and bar increment.

Sort

Write your sort algorithm here. Write it the way you normally would, and then modify it with animation function calls. For example, here is how Insertion Sort would be done.

Sort( )
{
   n = L.GetSize( );
   for (long i=1 ; i<n ; i++)
   {
      x = L[i];
      for (long j=i-1 ; j>=0 && L[j] > x ; j--)
      {
         L[j+1] = L[j];
      }
      L[j+1] = x;
   }
}

First we fix those places where we are assigning something to L[j+1]. The value list cannot be manipulated directly. Here's how we do this. We need to have at least one temp created in the CxxxCtrl constructor.

Sort( )
{
   n = L.GetSize( );
   for (long i=1 ; i<n ; i++)
   {
      x = L[i];
      L.MoveBarToTemp(i,0);
      for (long j=i-1 ; j>=0 && L[j] > x ; j--)
      {
         L.SwapBars(j,j+1); // color "None" is also moved
      }
      L.MoveTempToBar(0,j+1);
   }
}

The sort algorithm will now work. Now we need to decide on a color animation scheme. We will use the following conventions. Unsorted bars will be colored Red, the default. Sorted bars will be colored Green. Here's how we do this. We don't need to worry about setting the Red color, since it is default.

Sort( )
{
   n = L.GetSize( );
   for (long i=1 ; i<n ; i++)
   {
      x = L[i];
      L.MoveBarToTemp(i,0);
      for (long j=i-1 ; j>=0 && L[j] > x ; j--)
      {
         L.SwapBars(j,j+1); // color "None" is also moved
      }
      L.MoveTempToBar(0,j+1);
      L.SetBarColor(j+1,Green);
   }
}

Let's do something more interesting. Let's set the bar in the temporary location to Yellow, and the bar being compared in the inner loop to Cyan (Blue-Green). That's also simple, here's how.

Sort( )
{
   n = L.GetSize( );
   for (long i=1 ; i<n ; i++)
   {
      x = L[i];
      L.SetBarColor(i,Yellow);
      L.MoveBarToTemp(i,0);
      for (long j=i-1 ; j>=0 && L[j] > x ; j--)
      {
         L.SetBarColor(j,Cyan);
         L.SwapBars(j,j+1); // color "None" is also moved
         L.SetBarColor(j+1,Red);
      }
      L.MoveTempToBar(0,j+1);
      L.SetBarColor(j+1,Green);
   }
}

Now we have a nice animation, unfortunately, if we run it we won't see anything. There are two reasons for this. First, setting bar colors doesn't update the display. All that happens is some internal parameters are set in the CSortAnimation object. Second, even if the display was updated with the color changes, the changes would happen so fast we couldn't see them. We need to update the display, and slow things down a bit so we can see them. To do this, we must call the Sync function at the appropriate places. The Sync function redraws the display and waits for the number of milliseconds specified in the m_interval parameter. Where you call the Sync function is a matter of taste, but it should give a good impression about the speed of your animation. Here is how I would add the calls to Sync. Note that Sync is a member of CxxxCtrl not CSortAnimation, so leave off the L. prefix.

Sort( )
{
   n = L.GetSize( );
   for (long i=1 ; i<n ; i++)
   {
      x = L[i];
      L.SetBarColor(i,Yellow);
      L.MoveBarToTemp(i,0);
      Sync( );
      for (long j=i-1 ; j>=0 && L[j] > x ; j--)
      {
         L.SetBarColor(j,Cyan);
         Sync( );
         L.SwapBars(j,j+1); // color "None" is also moved
         Sync( );
         L.SetBarColor(j+1,Red);
         Sync( );
      }
      L.MoveTempToBar(0,j+1);
      L.SetBarColor(j+1,Green);
      Sync( );
   }
}

If you have text lines, you animate them using the functions L.HighLightText(x) and L.RemoveHighLight(x), where x is the line whose color you wish to change. Remember to use Sync calls, or the color changes won't be visible.