/** This program is intended to be used with the book "Java 7 -- What Do You Want To Do?"
 * by Steven P. Warr and is distributed free of charge and without and expectations of remuneration.
 * Chapter ALG   NOTE most program names begin with the title of the chapter in which
 * they are intended to be used.   Copyright 2010 Steven P. Warr All rights reserved
 */

//alg 06
import java.util.*; 
import java.io.*; 
public class Alg05 
{
   public static void main(String[ ] args) throws IOException
    {
    	Scanner infile = new Scanner(new File("alg05.data"));
		Scanner keyIn = new Scanner(System.in);
		int x = 0;
		int totalNumber = 0;
		totalNumber = infile.nextInt(); //First numebr in data file is the 
										//is the count.
		int [] number = new int[totalNumber];										
		System.out.println("Original array" + " " + totalNumber);
		while (infile.hasNext())
		     {
				number[x] = infile.nextInt();
				System.out.println(number[x]);
				x++;
			}	
		selectionSort(number);
		System.out.println("\nsorted array"+ " " + totalNumber);
		for(x = 0; x< number.length; x++)
			System.out.println(number[x]);
		
		
     }		
public static void selectionSort(int [] list)
  {
       int max = 0;
       for (int x = 0; x< list.length - 1; x++)
            {
		max = findMax(list,x);
 		if (list[x] != max)
                swap(list,x,max);
	}

 }	
public static int findMax(int[] list, int start)
{
       int maxIndex = start;
       for (int x = start + 1; x < list.length; x++)
		 if (list [x] < list [maxIndex])
			maxIndex = x;
       return maxIndex;
}
public static void swap(int[] list, int x , int y)
{
	int temp = list [x];    //The same swap algorithm as in sesequential sort 
	list[x] =list[y];     	//above, but in a separate method.
	list[y]= temp;      	
}
}
