// select sort public class selectsort { public static void main(String args[]) { int[] nums = {3,9,6,1,2}; Arrays AN = new Arrays(nums); AN.sort(); AN.print(); } } class Arrays { private int size; private int[] unsorted; private int[] sorted; public Arrays(int[] array) { unsorted = array; // store array inside object size = array.length; sorted = new int[size]; // create new array to store sorted values copy_array(); // sorted = unsorted; // think this should work, instead? try.... } // constructor Arrays // Want to save old array, but need same values to sort. // Can't use original and can't use alias "sorted = unsorted" // because both ideas would mess up original array. private void copy_array() { for (int i=0;i<=size-1;i++) sorted[i]=unsorted[i]; } // method copy_array public void sort() { int min, temp, index, scan; // outer loop checks each position for(index = 0; index < size-1; index++) { min = index; // inner loop finds smallest value for(scan = index+1; scan < size; scan++) if (sorted[scan] < sorted[min]) min = scan; // swap values temp = sorted[min]; sorted[min] = sorted[index]; sorted[index] = temp; } } // method sort // service method for printing arrays public void print() { print(0); // print original print(1); // print sorted } // method print // support method for printing arrays private void print(int test) { int[] array = new int[size]; // choose which array to print if (test == 0) { array = unsorted; System.out.print("Original array:"); } else if(test == 1) { array = sorted; System.out.print("Sorted array:"); } else { System.err.println("Unknown array type!"); System.exit(0); } // print array for(int i=0 ; i<=array.length-1;i++) System.out.print(" "+array[i]); System.out.println(); } // method print } // class Arrays