/*
 * Created on Oct 17, 2004
	 * 
	 * CS 178 Programming with Multimedia Objects.
	 *
	 * This example illustrates selection sort method.
	 */

	/**
	 * @author Aditya Mathur
	 */

	import java.applet.*;
	import java.awt.*;
	import java.awt.event.*;

	public class SelectionSort extends Applet implements ActionListener {
		
		
		// Array to hold data.
		
		final int maxElements=5;
		int [] dataArray=new int [maxElements];  // Create array. not initialized.
		TextField[] arrayDisplay=new TextField[20]; // Used to display sorted array.

		int currentSize=0; // Currently the array has 0 elements.
		
		// Create buttons and text boxes.
		
		Button sortAsc=new Button("SORT_ASC");
		Button sortDsc=new Button("SORT_DSC");
		Button addToArray=new Button("SAVE");
		Button emptyTheArray=new Button ("EMPTY");
		
		TextField dataInput=new TextField("Data" , 7);
		TextField dataOutput=new TextField("Array Empty", 15);  
		
		// Initialize the applet.
		
		public void init(){
			
			// Add listeners to buttons..
		
			emptyTheArray.addActionListener(this);
	
			sortAsc.addActionListener(this);
			sortDsc.addActionListener(this);
			addToArray.addActionListener(this);
			
//			 Add buttons and text fields to the applet screen.
			
			add(emptyTheArray); add(sortAsc);  add(sortDsc); add(addToArray);
			add(dataInput);
			add(dataOutput);
			for(int i=0; i<maxElements;i++){
				arrayDisplay[i]=new TextField("None", 7);
				add(arrayDisplay[i]);
			}
			
			setBackground(Color.orange);
			
		}
		
		
		// Process events.
		
		public void actionPerformed(ActionEvent e){
			
			Object buttonPressed=e.getSource();  // Get the name of the button pressed by the user.
			
			if(buttonPressed==emptyTheArray)emptyArray();
			else if(buttonPressed==sortAsc){
				sortArrayA();
			}
			else if(buttonPressed==sortDsc){
				sortArrayD();
			}
			else if(buttonPressed==addToArray)processAddToArray();
			}
		
		
		public void emptyArray(){
			dataOutput.setText("Array Empty.");
			dataInput.setText("Data");
			for(int i=0; i<currentSize; i++){
				arrayDisplay[i].setText("None");
				arrayDisplay[i].setBackground(Color.white);
			}
			currentSize=0;
		}

		// Sort the array in ascending  order.
		public void sortArrayA(){

			for(int pass=0;pass<currentSize; pass++){
				for (int j=0; j<currentSize-1; j++){
					if(dataArray[j]>dataArray[j+1])exchange(j, j+1);
				}
			}
			displaySortedArray();
		}

		// Sort the array in descending  order.
		public void sortArrayD(){

			for(int pass=0;pass<currentSize; pass++){
				int minIndex=findMinIndex(0,currentSize-pass-1); // Find index of the smallest element.
				exchange(minIndex,currentSize-pass-1);
			}
			displaySortedArray();
		}
		
//		 Sort the array in descending  order.
		public void sortArrayCorrect(){

			for(int pass=0;pass<currentSize; pass++){
				int minIndex=findMinIndex(0,currentSize-pass-1); // Find index of the smallest element.
				exchange(minIndex,currentSize-pass-1);
			}
			displaySortedArray();
		}
			
		
		// Exchange two elements of an array located at positions k and l.
		
	public void exchange(int k, int l){
		int temp;
		temp=dataArray[k];
		dataArray[k]=dataArray[l];
		dataArray[l]=temp;
	}

	public void displayArray(){
		String allElements=new String("");;
		for(int i=0; i<currentSize; i++){
			allElements=allElements+" "+ Integer.toString(dataArray[i]);
		}
		dataOutput.setText(allElements);
	}

	public void processAddToArray(){
		
	String dataTyped=dataInput.getText(); // Get data typed by the user.
		if(currentSize==maxElements){
			dataOutput.setText("Sorry, the array is full.");
			return;
		}
		currentSize++;
		int data=Integer.parseInt(dataTyped);
		dataArray[currentSize-1]=data;
		dataOutput.setText("Data saved:"+dataInput.getText());
		dataInput.setText("Data");
		if(currentSize==maxElements){
			dataOutput.setText("Data saved: "+dataTyped+"   Array full.");
			dataInput.setText("Data");
		}

	}
	
	
	// Find and return the index where the minimum element is located.
	
	public int findMinIndex(int start, int end){
		int min=dataArray[start];
		int index=start;
		if(start==end)return(start);
		
		for(int i=start+1; i<=end; i++){
			if(min>dataArray[i]){
				min=dataArray[i];
				index=i;
			}
		}
		return(index);
	}
	
	// Display array being sorted (or already sorted).
	
public void displaySortedArray(){
	for(int i=0;i<currentSize; i++){

		arrayDisplay[i].setText(Integer.toString(dataArray[i]));
		arrayDisplay[i].setBackground(Color.yellow);
	}
}
	}
