/************************************************************
 from Savitch
 Class for sorting an array of ints from smallest to largest.
 Precondition: Every indexed variable of the array a has a value.
 ************************************************************/
public class SelectionSort
{
public static void sort(int[] a)
    {
	int index, indexOfNextSmallest;
	for (index = 0; index < a.length - 1; index++)
		{
		indexOfNextSmallest = indexOfSmallest(index, a);
		interchange(index,indexOfNextSmallest, a);
		}
    } // end sort int method

public static void sort(String[] a)
    {
	int index, indexOfNextSmallest;
	for (index = 0; index < a.length - 1; index++)
		{
		indexOfNextSmallest = indexOfSmallest(index, a);
		interchange(index,indexOfNextSmallest, a);
		}
    } // end sort String method
    
public static void reverseArray(String[] a)
    {
	int index;
	for (index = 0; index < a.length; index++)
        {
		System.out.println(index+ " " + (a.length-index));
		if (index < a.length/2)
		   {
		   interchange(index, a.length-index-1, a);
           }
		}
    } // end reverse

private static void interchange(int i, int j, int[] a)
    {
	int temp;
	temp = a[i];
	a[i] = a[j];
	a[j] = temp;
    }

private static void interchange(int i, int j, String[] a)
    {
	String temp;
	temp = a[i];
	a[i] = a[j];
	a[j] = temp;
    }

/**************************************************
Returns the index of the smallest value among
a[startIndex], a[startIndex+1], ... a[a.length-1]
**************************************************/
private static int indexOfSmallest(int startIndex, int[] a)
   {
   int min = a[startIndex];
   int indexOfMin = startIndex;
   int index;
   
   for (index = startIndex + 1; index < a.length; index++)
      {
	  
	  if (a[index] < min)
		 {
		 min = a[index];
		 indexOfMin = index;
         } // end if
	  
	  } // end for

   return indexOfMin;
   }

private static int indexOfSmallest(int startIndex, String[] a)
   {
   int min = a[startIndex].compareToIgnoreCase("A"); // a "score" indicating nearest to A
   int indexOfMin = startIndex;
   int index;
		
   for (index = startIndex + 1; index < a.length; index++)
      {
	  if (a[index].compareToIgnoreCase("A") < min)
	     {
		 min = a[index].compareToIgnoreCase("A");
		 indexOfMin = index;
		 } // end if
	/*	 
	  else if (a[index].compareToIgnoreCase("A") == min)
	     {
		 if (a[index].substring(1,2).compareToIgnoreCase("A") < a[indexOfMin].substring(1,2).compareToIgnoreCase("A"))
		    {
		    min = a[index].compareToIgnoreCase("A");
		    indexOfMin = index;
		    } 
		 } 
	*/   	 
	   } // end for
	
	return indexOfMin;
    } // end method indexOfSmallest (int, String)



} // end class


