/* We want a Tile class that models tiled windows on a screen desktop.
* Tiled windows are rectangles plus a "z-order".  Windows with a larger
* z-order are displayed in front of those with a smaller z-order. 
* To reuse code, we inherit Tile from Rectangle, a class that is already
* defined in the java.awt package.
* Note that we cannot now inherit from the class Sortable since we are already
* inheriting from Rectangle.  Use an Interface instead
*/

import java.awt.*;

public class TileTest
{  public static void main(String[] args)
   {  Tile[] a = new Tile[20];

      int i;
      for (i = 0; i < a.length; i++) 
         a[i] = new Tile(i, i, 10, 20, 
            (int)(100 * Math.random()));
              
      Sort.shell_sort(a);
      
      for (i = 0; i < a.length; i++) 
         System.out.println(a[i]);
   }
}

interface Sortable 
{  public int compare(Sortable b);
}

class Sort
{  public static void shell_sort(Sortable[] a)
   {  int n = a.length;
      int incr = n / 2;
      while (incr >= 1)
      {  for (int i = incr; i < n; i++)
         {  Sortable temp = a[i];
            int j = i;
            while (j >= incr 
               && temp.compare(a[j - incr]) < 0)
            {  a[j] = a[j - incr];
               j -= incr;
            }
            a[j] = temp;
         }
         incr /= 2;
      }
   }
}

class Tile extends Rectangle implements Sortable
{  public Tile(int x, int y, int w, int h, int zz)
   {  super(x, y, w, h);
      z = zz;
   }
   
   public int compare(Sortable b)
   {  Tile tb = (Tile)b;
      return z - tb.z;
   }
   
   public String toString()
   {  return super.toString() + "[z=" + z + "]";
   }

   private int z;
}


/*
 * Cay S. Horstmann & Gary Cornell, Core Java
 * Published By Sun Microsystems Press/Prentice-Hall
 * Copyright (C) 1997 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. 
 * 
 * THE AUTHORS AND PUBLISHER MAKE 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. THE AUTHORS
 * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
/**
 * @version 1.00 07 Feb 1996 
 * @author Cay Horstmann
 */

