// The "Insertion" class.
import hsa.Console;

public class Insertion
{
    //a class that illustrates the insertion sort
    static Console c = new Console();
    
    void insertSort(int[] v, int n)
{
    //insertion sort method
    int i,current= 0;//i is a counter and current is a variable to hold 
	//an array value so it can be replaced
    int j= n;// another counter
    for (i= 2; i<=n; i++)//repeat this loop starting at i=2
    {
	current= v[i];//save the value at  position i in the array
	j= i;//set the counter j to be the value of i
	while( (j>=1) && (current<v[j-1]))//look for a value greater than current
	{
	    v[j]= v[j-1];//move the values less than current down to make room for current
	    j= j-1;
	}
    v[j]= current;//save the value of current into v[j]
    
    }
	
}
    
    public static void main (String[] args)
    {
	Insertion member = new Insertion();//to call methods
	int i, number= 0;//i is a counter and number is how many values
	int[] values;//an array of values
	c.println("How many numbers do you want to put in order?");
	number= c.readInt();//read in how many values you need to sort
	values= new int[number+1];//set the bounds of the array to hold the values
	c.println("Enter the values one per line.");
	for (i= 1; i<=number; i++)//read in the values
	    values[i]= c.readInt();
	member.insertSort(values, number);//call the sorting method
	c.println("The sorted values are  ");
	for (i= 1; i<=number; i++)//print out the sorted values
	    c.println(values[i]);
	
    } // main method
} // Insertion class

