Computer Science 121
An example of selection sort and bubble sort.

import java.io.*;

public class SortArray
{
    public static void main (String [] args)
    {
        ArrayClass list = new ArrayClass ();
        list.fillList ();
        list.selectionSort ();
        list.displayList ();
    } // method main
} // class SortArray

/* A class that fills an array with integers and then uses either a selection sort or a bubble sort to order it. */
class ArrayClass
{
    private final int maxSize = 10;
    private int [] list;
    private int listSize;

    public ArrayClass ()
    {
        list = new int [maxSize];
        listSize = 0;
    } // constructor

    // Method to read integers from the keyboard and store them in an array.
    public void fillList ()
    {
        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        try
        {
            System.out.print ("Item: ");
            int item = Integer.parseInt (stdin.readLine ());
            while ((item != 0) && ( listSize < maxSize))
            {
                list [listSize] = item;
                listSize ++;
                System.out.print ("Item: ");
                item = Integer.parseInt (stdin.readLine ());
            } // while
        } catch (IOException e) {}
    } // method fillList

    // Method to display the contents of the array on the screen.
    public void displayList ()
    {
        for (int count = 0; count < listSize; count ++)
            System.out.print (" " + list [count]);
        System.out.println ("");
    } // method displayList

    // A selection sort method.
    public void selectionSort ()
    {
        int small, locSmall;
        for (int pass = 0; pass < listSize -1; pass ++)
        {
            small = list [pass];
            locSmall = pass;
            for (int count = pass + 1; count < listSize; count ++)
            {
                if (list [count] < small)
                {
                    small = list [count];
                    locSmall = count;
                }
            }
            list [locSmall] = list [pass];
            list [pass] = small;
        }
    } // method selectionSort

    // A bubble sort method.
    public void bubbleSort ()
    {
        int temp;
        boolean success;
        do
        {
            success = true;
            for (int count = 0; count < listSize-1; count ++)
            {
                if (list [count] > list [count+1])
                {
                    temp = list [count];
                    list [count] = list [count+1];
                    list [count+1] = temp;
                    success = false;
                }
            }
        } while (!success);
    } // method bubbleSort
} // class ArrayClass