/** Sample program for CISC 121, Fall 2000. * This program reads a list of up to 10 integers, sorts them in ascending * order, and prints them out. It uses the bubble sort algorithm. *
* Input: The integers to be sorted. These are read from the standard input. * The program repeatedly prompts for an integer, then asks the user if * s/he is done. This repeats until the user is done, or until the * maximum number of integers is reached. If the user enters characters * that are not integer format, the program will ask him/her to try * again. *
* Output: The sorted list of integers, on the standard output. *
* Libraries Used: SavitchIn class for input. *
* Known Limitations: When the user answers the "are you done?" question, any * character other than 'n' is considered a yes. * @author Margaret Lamb, tutorial X */ public class Bubble { /** * The maximum number of integers the program will accept */ private static final int MAX_SIZE = 10; /** * prompts for and reads an array of integers from the standard * input. Repeatedly prompts for an integer, then asks the user if * s/he is done. This repeats until the user is done, or until the * maximum number of integers is reached. If the user enters characters * that are not integer format, the method will ask him/her to try * again. *
* Limitation: If the user answers the "are you done?" question with
* any character but a lower-case 'n', it is considered a yes.
*
* @param inputArray the array into which the integers are read
* @return the number of integers read
*/
private static int readArray(int inputArray[]) {
int intCount = 0; // number of integers read so far
char doneAnswer; // user's answer to "are you done?" question
// set doneAnswer to make sure the loop will execute once
doneAnswer = 'n';
// Repeat this loop until the user says done OR until we get the maximum
// number of integers.
while (doneAnswer == 'n' && intCount < MAX_SIZE) {
System.out.print("enter an integer: ");
// If the user enters characters that do not make up an integer,
// the readLineInt takes care of printing an error message and
// re-prompting. So we don't have to worry here about the legality
// of the integer.
inputArray[intCount] = SavitchIn.readLineInt();
intCount++;
// Test intCount here to avoid an "are you done?" question if we've
// already read the maximum number of integers. In this case, we'll
// be stopping anyway.
if (intCount < MAX_SIZE) {
System.out.print("are you done? (y/n): ");
doneAnswer = SavitchIn.readLineNonwhiteChar();
} // end if
} // end while
return intCount;
} // end inputArray
/**
* sorts an array of integers in ascending order using the
* "bubble sort" algorithm.
*
* assumptions: * 1. size is not negative * 2. the array has least size elements ** * @param theArray the array to be sorted * @param size the number of elements in the array to be sorted */ private static void sortArray(int theArray[], int size) { // The bubble sort algorithm makes size-1 passes through the array. In // each pass, it swaps adjacent pairs of elements if they are in the // wrong order. int pass; // the number of the pass we're currently on int pairStart; // the position of the first element of the pair being // tested and possibly swapped int tempInt; // a temporary integer used for swapping integers for (pass = 0; pass < size-1; pass++) { for (pairStart = 0; pairStart < size-pass-1; pairStart++) { // If this pair is out of order, swap them if (theArray[pairStart] > theArray[pairStart+1]) { tempInt = theArray[pairStart]; theArray[pairStart] = theArray[pairStart+1]; theArray[pairStart+1] = tempInt; } // end if } // end for pairStart } // end for pass } // end sortArray /** * prints an array of integers on the standard output, one per * line. *
assumptions: size is not negative, and the array has at least size * elements * * @param theArray the array to be printed * @param size the number of elements in the array to be printed */ private static void printArray(int theArray[], int size) { for (int i = 0; i < size; i++) System.out.println(theArray[i]); } // end printArray /** * the main method for the program. Creates an array of integers, * then calls other methods to read, sort, and print them. * * @param args The command-line arguments. Not used in this program, * but required by Java */ public static void main(String args[]) { // the array that will hold the integers to be sorted int sortingArray[] = new int[MAX_SIZE]; int arraySize; // the number of integers actually in the array // read the array arraySize = readArray(sortingArray); // sort the array sortArray(sortingArray, arraySize); // print the sorted array System.out.println(); // skip a line System.out.println("Sorted Result:"); printArray(sortingArray, arraySize); } // end main } // end class Bubble