/*
 * @(#)InsertionSortAlgorithm.java       1.0 99/07/21
 *
 * Copyright (c) 1999 Marco Lizza. All Rights Reserved.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/**
 * An insertion sort demonstration algorithm
 * InsertionSortAlgorithm.java, Wed Jul 21 10:32:35 1999
 *
 * @author Marco Lizza
 * @version     1.0, 21 Jul 1999
 */

class InsertionSortAlgorithm extends SortAlgorithm {
    void sort(int a[]) throws Exception {
        int i, j ,key;

        for (j = 1; j < a.length; j++) {
            if (stopRequested)
                return;

            key = a[j];

            for (i = (j - 1); ((i >= 0) && (a[i] > key)); i--) {
                a[i + 1] = a[i];
                pause(i + 1, j);
            }

            a[i + 1] = key;
            pause(i + 1, j);
        }
    }
}
