/*
 * @(#)CombSort11Algorithm.java   1.0 96/11/20 Jason Harrison
 *
 * Copyright (c) 1995 University of British Columbia
 *
 * 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.
 *
 * UBC 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. UBC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

/** A comb sort 11 sort demonstration algorithm. <br />
 *  <br />
 *  Idea from Byte April 1991 by Richard Box and Stephen Lacey
 *  Michele Estebon (mestebon@vt.edu) brought this to my attention.<br />
 *  <br />
 *  Author Jason Harrison; his versvion: 1.0, 20 Nov 1996<br />
 *  <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 CombSort11Algorithm extends SortApplet.SortAlgorithm {
   final float SHRINKFACTOR = 1.3F;
   
   @Override void sort(int[] a) {
      boolean flipped = false;
      int gap, top;
      int i, j;
      
      gap = a.length;
      do {
         gap = (int) (gap / SHRINKFACTOR);
         switch (gap) {
            case 0: /* the smallest gap is 1 - bubble sort */
               gap = 1;
               break;
            case 9: /* this is what makes this Combsort11 */
            case 10: 
               gap = 11;
               break;
            default: break;
         } // switch
         flipped = false;
         top = a.length - gap;
         for (i = 0; i < top; ++i) {
            if (stopRequested)  return; // Abbruch
            j = i + gap;
            if (a[i] > a[j]) {
               int T = a[i];
               a[i] = a[j];
               a[j] = T;
               flipped = true;
            }
            pause(i,j);
         }
      } while (flipped || (gap > 1));
      /* like the bubble and shell sorts we check for a clean pass */
   } // sort(int[])
   
} // class  CombSort11Algorithm (15.11., 27.11.2005, A.W.)


