/******************************************************************
 * Program: ArraySorter
 *
 * Programmer: Tyler Johnson
 *
 * Due Date: N/A

 *
 * COMP 110-001        Instructor: Tyler Johnson
 *
 * Description: The ArraySorter class provides functionality for
 *				sorting arrays of integers
 *
 *
 * Input: N/A
 *
 * Output: N/A
 *
 ******************************************************************/


public class ArraySorter  {

	/*
	 *  printArray
	 *
	 *  Prints the contents of an integer array out to screen
	 */
	
	public static void printArray(int[] array)  {
	
		System.out.print("{");
		
		for(int i = 0; i < array.length - 1; i++)  {
			System.out.print(array[i] + ", ");
		}
		
		System.out.print(array[array.length-1]);
		
		System.out.println("}");		
	} 
	
	public static void selectionSort(int[] array)  {
	
		for(int i = 0; i < array.length; i++)  {
			int smallestInd = indexOfSmallest(array, i);
			swap(array, i, smallestInd);		
		}
	}
	
	private static int indexOfSmallest(int[] array, int startInd)  {
	
		int smallest = array[startInd];
		int indexOfSmallest = startInd;
		 
		for(int i = startInd; i < array.length; i++)  {
			
			if(array[i] < smallest)  {
				smallest = array[i];
				indexOfSmallest = i; 
			}
		}
		
		return indexOfSmallest;	
	}
	
	private static void swap(int[] array, int a, int b)  {
	
		int temp = array[a];
		array[a] = array[b];
		array[b] = temp;
	}

	public static void main(String[] args)  {
	
		int[] b = {6, 4, 44, 2, 11, 77, 4, 5, 90};
		System.out.println("before:");
		printArray(b);
		ArraySorter.selectionSort(b);
		System.out.println("after:");
		printArray(b);
	}
}