import java.util.ArrayList;
/**
* Sorting Algorithms Lecture:
* An object of this class sorts data using a Selection Sort.
*
* @author Alyce Brady
* @version 16 February 2016
*/
public class SelectionSorterComplete implements Sorter
{
// State: instance variables go here.
private String description;
// Constructor
/**
* Constructs a new object of this class.
*/
public SelectionSorterComplete()
{
// initialise instance variables
this.description = "Selection Sort";
}
// Methods
/**
* Describes which sorting algorithm this is.
*/
public String getDescription()
{
return this.description;
}
/**
* Sorts the data in the ArrayList of data.
*/
public void sort(ArrayList data)
{
// Put smallest value at beginning, next-smallest next, etc.
for ( int startIndex = 0; startIndex < data.size() - 1; startIndex++ )
{
// Find the minimum value between startIndex and the end.
int minIndex = startIndex;
for ( int k = startIndex + 1; k < data.size(); k++ )
{
if ( data.get(k).compareTo(data.get(minIndex)) < 0 )
{
minIndex = k;
}
}
// Swap the minimum value and the start of the sub-range.
swap (data, startIndex, minIndex);
}
}
/**
* Swaps the values at index1 and index2 in the ArrayList of data.
*/
private void swap(ArrayList data, int index1, int index2)
{
String temp = data.get(index1);
data.set(index1, data.get(index2));
data.set(index2, temp);
}
}