
import java.util.Vector;

/**
 Class for sorting a vector of Strings lexicographically
*/
public class StringSelectionSort
{
  /**
   Sorts the vector a so that v.get(0), v.get(1), . . . 
   v.get(a.size( ) - 1) are in lexicographic order.
   Assumes the vector contains only Strings.
  */
   public static void sort(Vector v)
   {
       int index, indexOfNextSmallest;
       for (index = 0; index < v.size( ) - 1; index++)
       {//Place the correct value in position index:
           indexOfNextSmallest = 
                           indexOfSmallest(index, v);
           interchange(index,indexOfNextSmallest, v);
           //v.get(0), v.get(1),...,
            //v.get(index) are sorted. The rest of
           //the elements are in the remaining positions.
       }
   }

   /**
    Precondition: i and j are legal indices for the vector a.
    Postcondition: The values of v.get(i) and
     v.get(j) have been interchanged.
   */
   private static void interchange(
                           int i, int j, Vector v)
   {
       Object temp;
       temp = v.get(i);
       v.set(i, v.get(j));
       v.set(j, temp);
   }

   /**
    Returns the index of the lexicographically first value among
    v.get(startIndex), v.get(startIndex+1),...,
    v.get(v.size( ) - 1)
   */
   private static int indexOfSmallest(
                                int startIndex, Vector v)
   {
       String min = (String)v.get(startIndex);
       int indexOfMin = startIndex;
       int index;
       for (index = startIndex + 1; 
                           index < v.size( ); index++)
           if (((String)(v.get(index))).compareTo(min) < 0)
           {
               min = (String)v.get(index);
               indexOfMin = index;
           }
       return indexOfMin;
   }
}
