/**
 * Import of ACM Lib
 */
import acm.program.*;

/**
 * SortAndPrintIntArray
 * 
 * Sorts and prints an IntArray
 * 
 * Uses BubbleSort Algorithm
 * 
 * @author Ulf Gebhardt
 * @version 0.1b
 */
public class SortAndPrintIntArray extends DialogProgram{
	
	/**
	 * Generated serialVersionUID
	 */
	private static final long serialVersionUID = 6320594908339909991L;
	/**
	 * Stores an Array of Int 
	 */
	private int[] intArray;
    
	/**
	 * Start Sort
	 */
	public void run()
	{
		setIntArray(new int[] { 0, 9, 4, 4, 6, 2, 8, 5, 1, 7, 3 });
		sortIntArray();
		intArraytoString();
		println(intArraytoString());
	}
  

	/**
	 * Switch two elements (i and j) in Array intArray
	 * 
	 * @param i Integer
	 * @param j Integer
	 */
	public void swapIntArrayElements(int i, int j) {
		int temp = intArray[i];
		intArray[i] = intArray[j];
		intArray[j] = temp;
	}

	/**
	 * Sort the Array intArray (BubbleSort)
	 */
	public void sortIntArray() {
		boolean sorted = true;
		while (sorted) {
			sorted = false;
			for (int i = 0; i < intArray.length - 1; i++)
				if (intArray[i] > intArray[i + 1]) {
					swapIntArrayElements(i, i + 1);
					sorted = true;
				}
		}
	}

	/**
	 * Returns String representation of intArray
	 * 
	 * @return String
	 */
	public String intArraytoString() {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < intArray.length; i++)
		{
			result.append(intArray[i]);
			result.append(" ");
		}    
		return result.toString();
	}
	
	/**
	 * Returns Attribute intArray
	 * 
	 * @return int[]
	 */
	public int[] getIntArray()
	{
	   return intArray;
	}
	
	/**
	 * Set Attribute intArray to value newarray
	 * 
	 * @param newarray int[]
	 */
	public void setIntArray(int[] newarray)
	{
		intArray = newarray;
	}

	/**
	 * Start Programm
	 * 
	 * (Sort IntArray)
	 * 
	 * @param args String[]
	 */
	public static void main(String[] args) {
		new SortAndPrintIntArray().start(args);
	}
}
