import java.util.Random; // needed to generate random integers /** *

A test-bed for exploring, developing, and testing ideas about * arrays of integers. *

*

Note that most (if not all) of the methods on this class are * static by design because we wish to explicitly * manipulate these arrays of integers. *

* @author tomr55 * */ public class SimpleArrays { private static Random generator = new Random(); /** *

Preconditions: None.

*

Postconditions: an array of_size is generated * and is populated with randomly generated integers; each * integer will be the range of [1 .. of_size]. *

* @param of_size * @return

a (possibly empty, if of_size=0) array of * randomly-generated ints. */ public static int[] genArray( int of_size ) { int[] returnArray = new int[ of_size ]; for( int i = 0 ; i < returnArray.length; i++ ) returnArray[ i ] = generator.nextInt( of_size ) + 1; return returnArray; } public static void printArray( int[] array ) { System.out.print("["); for( int thisInt : array ) System.out.print( thisInt + ", " ); System.out.println("]"); } public static int[] selectSort( int[] array ) { for( int index = 0; index < array.length; index++ ) { int index_of_smallest = smallestIndex( index, array ); swap( index, index_of_smallest, array ); } return array; } private static void swap(int to, int from, int[] array) { int temp = array[ to ]; array[ to ] = array[ from ]; array[ from ] = temp; } private static int smallestIndex(int start, int[] array) { int index_of_smallest = start; while( start < array.length ) { if( array[ start ] < array[ index_of_smallest ]) { index_of_smallest = start; } start++; } return index_of_smallest; } public static void main( String[] args ) { int[] testArray = genArray(100); System.out.println("Generated "); printArray(testArray); selectSort(testArray); System.out.println("Sorted "); printArray( testArray ); } }