import java.util.Random; public class Sorter { protected int[] data; public Sorter(int numItems){ this(numItems, -1); } public Sorter(int numItems, int seed){ data = new int[numItems]; //randomize the array Random rand = null; if(seed < 0) rand = new Random(); else rand = new Random(seed); for(int i = 0; i < data.length; i++){ data[i] = rand.nextInt(); } } /* public void swap(int i, int j){ int temp = data[i]; data[i] = data[j]; data[j] = temp; } */ //check if an array is sorted //make sure every element is greater than or equal to the one before it. public boolean isSorted(){ if(data.length <= 1) return true; else { int trailer = data[0]; for(int i = 1; i < data.length; i++){ if(trailer > data[i]){ return false; } trailer = data[i]; } } return true; } //selection sort public void sort(){ //for each item in the array, find the minimum of what //is left and store it at the current index for(int i = 0; i < data.length; i++){ data[i] = findMinIndex(i, data.length-1); } } //find the index of the minimum value between indexes start and end protected int findMinIndex(int start, int end){ int minIndex = start; for(int i = start+1; i <= end; i++){ if(data[i] < data[minIndex]){ minIndex = i; } } return minIndex; } }