tuamigo

Untitled

Nov 26th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. /// Sort the array between a low and high bound in either ascending or descending order
  2. /// Parameters:
  3. ///      int  nFirst : between 0 and the upper bound of the array.
  4. ///      int  nLast  : between 0 and the upper bound of the array.
  5. ///                  : must be guaranteed >= nFirst.
  6. ///      bool bAscend: true  - sort in ascending order
  7. ///                  : false - sort in descending order
  8.         private bool ExecuteActualSort(int nFirst, int nLast, bool bAscend)
  9.         {
  10.             bool bSortOK = false;
  11.  
  12.             try
  13.             {
  14.                 int i, j, nStkPtr = 0, nTmp;
  15.                 bool bSortCompleted = false, bDirection = true;
  16.  
  17.                 // get the maximum size of stack required:
  18.                 int nStackMax = (int)((Math.Log(nLast) + 3) * 2); // from Knuth Vol 3.
  19.                 // Note, +3 is added because:
  20.                 // +1 to round up rather than down,
  21.                 // +1 because it's a full bottom-up stack (ie Stack[0] is never used),
  22.                 // +1 because data array is zero-indexed.
  23.  
  24.                 int[,] nStack = new int[nStackMax, 2];
  25.  
  26.                 do
  27.                 {
  28.                     do
  29.                     {
  30.                         i = nFirst;
  31.                         j = nLast;
  32.                         bDirection = true;
  33.  
  34.                         do
  35.                         {
  36.                             if ((nData[i] > nData[j]) == bAscend)
  37.                             {
  38.                                 // Swap the two items in the list pointed to by i and j
  39.                                 nTmp = nData[i];
  40.                                 nData[i] = nData[j];
  41.                                 nData[j] = nTmp;
  42.                                 bDirection = !bDirection;
  43.                             }
  44.  
  45.                             if (bDirection)
  46.                                 j--;
  47.                             else
  48.                                 i++;
  49.  
  50.                         }
  51.                         while (i < j);
  52.  
  53.                         if (i + 1 < nLast)
  54.                         {
  55.                             // There's another partition to be sorted
  56.                             nStkPtr++;
  57.                             nStack[nStkPtr, 0] = i + 1;
  58.                             nStack[nStkPtr, 1] = nLast;
  59.                         }
  60.                         nLast = i - 1;
  61.  
  62.                     }
  63.                     while (nFirst < nLast);
  64.  
  65.                     if (nStkPtr == 0)
  66.                     {
  67.                         // No more partitions to sort, so by definition we've finished!
  68.                         bSortCompleted = true;
  69.                     }
  70.  
  71.                     else
  72.                     {
  73.                         // Pop the most recently stored partition and sort that
  74.                         nFirst = nStack[nStkPtr, 0];
  75.                         nLast = nStack[nStkPtr, 1];
  76.                         nStkPtr--;
  77.                     }
  78.                 }
  79.  
  80.                 while (!bSortCompleted);
  81.  
  82.                 bSortOK = true;
  83.             }
  84.  
  85.             catch { }
  86.  
  87.             return bSortOK;
  88.         }
Advertisement
Add Comment
Please, Sign In to add comment