// Presumably "NUM" has been defined above or in a file #included
// For instance, "typedef long int NUM;"

// Insertion sort algorithm
void InsSort (NUM *Array,         // Array to be sorted
              int  Dim)           // Number of entries in the array
{  if ( Dim > 1 )
   {//Start-up:
      int Hole = Dim-1;           // Last subscript in the array
      NUM Save = Array[Hole];     // Value to be positioned

      InsSort (Array, Dim-1);     // Put into order up through [Dim-2]

//    Then position Value from [Dim-1] in [0] to [Dim-1]
      for ( ; Hole > 0 && Array[Hole-1] > Save ; Hole-- )
         Array[Hole] = Array[Hole-1];  // Move out of the way

      Array[Hole] = Save;         // Plug the hole, where ever it is.
   }
// else we have nothing to do!
}

// Functions used by the Selection sort algorithm:  Swap and MaxIndex

// Interchange two NUMs
void Swap (NUM &L, NUM &R)
{ NUM T = L; L = R; R = T;  }

// Subscript of the largest entry in X
int MaxIndex (NUM *X, int N)
{  int Big = 0,
       Idx;

   for ( Idx = 1; Idx < N; Idx++ )
      if ( X[Big] < X[Idx] )
         Big = Idx;
   return Big;
}

// Selection sort algorithm
void SelSort (NUM *Array,         // Array to be sorted
              int  Dim)           // Number of entries in the array
{  if ( Dim > 1 )
   {
      int Big = MaxIndex (Array, Dim);

      Swap ( Array[Big], Array[Dim-1] );
      SelSort (Array, Dim-1);
   }
// else we have nothing to do!
}
