// The "SelectSorter" class.
import hsa.Console;

public class SelectSorter implements Sorting_Machine
{
    public  static Console c = new Console();
    static int [] keeper;
    boolean accepting;
    static int total;
    static int current = 0;
    
    public SelectSorter ()
    {
	c.println("How many integers do you want to sort?");
	total = c.readInt();
	keeper = new int [total];
	accepting = true;
	
    }
    
    public void addEntry (int x)
    {
	 keeper[current]= x;
     
	 current++;
    }
    
    public void changeModes ( )
    {
	Select smember = new Select ();
	
	smember.selectSort(keeper, total);
	accepting = false;
    }
    
    public int getSmallest ()
    {
	 return keeper[current];
	 
    }
    
    public int entryCount()
    {
	return current;
    }
    
    public boolean isAccepting()
    {
	return accepting;
    }
    
    public static void main (String[] args)
    {
      SelectSorter srt = new SelectSorter();  
      
      for (int i=0; i<srt.total; i++)
      {
	c.println("next?");
	srt.addEntry(c.readInt());
      }
     current--;
      srt.changeModes();
      for (int i =0; i<total; i++)
      {
	int nextvalue = 0;
	//c.println(keeper[i]);
	nextvalue = srt.getSmallest();
	c.println(nextvalue);
	current--;
      }
      
    } // main method
    
} // SelectSorter class

public class Select
{
    
    static int getMinIndex (int [] v, int first, int last)
    //find the index of the smallest element starting at first and
    //ending at last
    {
	int minInd = first;//start the min index at 1
	int i = first;//start i at first
	while (i <= last)//repeat the loop from i to last
	{
	    
	    if (v [i] > v [minInd])//check to see if any array values
				//are less than the value at the minInd
		minInd = i;//if so, replace minInd with the newly found minimum
			    //index
	    i = i + 1;//increment i
	}
	return minInd;//return the index of the smallest element between
			//first and last
    }


    void selectSort (int [] v, int n)
    //put the values of v in order using the selection approach
    {
	int i = 0;//to use as a counter
	int temp = 0;//to store a value temporarily for swapping
	int minI = 0;//to hold the index of the smallest element

	for (i = 0 ; i < n ; i++)//repeat for each element in ithe array
	{
		     
	    minI = getMinIndex (v, i, n-1);//use the method getMinIndex to find out where
	    //the smallest element is starting at the ith entry
	    
	    temp = v [i];//save the ith element
	    v [i] = v [minI];//replace the ith element 
	    v [minI] = temp;//use the temp to put in the minI element
	    
	}

    }

}




