/*
 * @(#)BubbleSortAlgorithm.java   1.6 95/01/31 James Gosling
 *
 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

/**  A bubble sort demonstration algorithm. <br />
 *  <br />
 *  SortAlgorithm.java, Thu Oct 27 10:32:35 1994
 *
 * Author: James Gosling; his version: 1.6, 31 Jan 1995<br />
 * Modified 23 Jun 1995 by Jason Harrison@cs.ubc.ca:
 *   Algorithm completes early when no items have been swapped in the 
 *   last pass.<br />
 *  Modif. 15.11.2005, Albrecht Weinert (a-weinert.de) :<br />
 *  {@link SortApplet.SortAlgorithm} + Kleinigkeiten.<br />  
 *  <br />
 *  @author   Albrecht Weinert
 *  @version  V.161, 28.05.2011
 */
public final class BubbleSort2Algorithm extends SortApplet.SortAlgorithm {
 
   @Override void sort(int[] a) {
      for (int i = a.length; --i>=0; ) {
         boolean flipped = false;
         for (int j = 0; j < i; ++j) {
            if (stopRequested) return; // Abbruch
            if (a[j] > a[j + 1]) {
               int temp = a[j];
               a[j] = a[j+1];
               a[j + 1] = temp;
               flipped = true;
            }
            pause(i,j);
            
         }
         if (!flipped)  return; // fertig
      }
   } // sort(int[])
   
} // class BubbleSort2Algorithm (15.11., 27.11.2005, A.W.)

