// Sort.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-10-09 File created. // 2005-03-27 Last modification. // Compile in MS-DOS window: javac Sort.java // Execute in MS-DOS window: java TestingSorting import java.util.* ; class TestingSorting { public static void main( String[] not_in_use ) { double[] array_of_numbers = new double[ 100 ] ; System.out.print( "\n This program sorts the numbers that you enter" + "\n from the keyboard. The program stops asking for new" + "\n numbers when you enter a letter. \n\n" ) ; int number_of_numbers_read = InputOutput.ask_numbers_to_array( array_of_numbers ) ; Sort.sort_to_ascending_order( array_of_numbers, number_of_numbers_read ) ; InputOutput.print_array_of_numbers( array_of_numbers, number_of_numbers_read ) ; } } class InputOutput { public static int ask_numbers_to_array( double[] array_of_numbers ) { Scanner keyboard = new Scanner( System.in ) ; int number_of_given_numbers = 0 ; System.out.print( " Enter a number: " ) ; boolean keyboard_input_is_numerical = true ; while ( keyboard_input_is_numerical == true && number_of_given_numbers < array_of_numbers.length ) { try { double number_from_keyboard = keyboard.nextDouble() ; array_of_numbers[ number_of_given_numbers ] = number_from_keyboard ; number_of_given_numbers ++ ; System.out.print( " Enter a number: " ) ; } catch ( Exception not_numerical_input_exception ) { keyboard_input_is_numerical = false ; } } return number_of_given_numbers ; } public static void print_array_of_numbers( double[] array_of_numbers, int number_of_numbers_in_array ) { for ( int number_index = 0 ; number_index < number_of_numbers_in_array ; number_index ++ ) { if ( ( number_index % 5 ) == 0 ) { System.out.print( "\n" ) ; } System.out.printf( "%10.3f", array_of_numbers[ number_index ] ) ; } } } class Sort { public static void sort_to_ascending_order( double[] array_of_numbers, int number_of_numbers_in_array ) { for ( int number_index = 0 ; number_index < number_of_numbers_in_array - 1 ; number_index ++ ) { int number_of_unsorted_numbers = number_of_numbers_in_array - number_index ; int index_of_smallest_number = get_index_of_smallest_number_in_array( array_of_numbers, number_index, number_of_unsorted_numbers ) ; double smallest_number = array_of_numbers[ index_of_smallest_number ] ; // Now we simply put the number in current array // position to where the smallest number is, and // then put the smallest number in current position. array_of_numbers[ index_of_smallest_number ] = array_of_numbers[ number_index ] ; array_of_numbers[ number_index ] = smallest_number ; } } static int get_index_of_smallest_number_in_array( double[] array_of_numbers, int index_of_first_number_to_process, int number_of_numbers_to_process ) { double smallest_number = array_of_numbers[ index_of_first_number_to_process ] ; int index_of_smallest_number = index_of_first_number_to_process ; for ( int number_index = index_of_first_number_to_process + 1 ; number_index < index_of_first_number_to_process + number_of_numbers_to_process ; number_index ++ ) { if ( array_of_numbers[ number_index ] < smallest_number ) { smallest_number = array_of_numbers[ number_index ] ; index_of_smallest_number = number_index ; } } return index_of_smallest_number ; } } /* NOTES: This program uses Java 2 Standard Edition, Version 1.5 features. Compilation with older Java compilers is thus not possible. */