/*---------------------------------------------------------------------------
  File:         		Sorting.java
Package:                        JFLAP Version 2.0
Author:				Magda & Octavian Procopiuc V1.0 07/15/96
                                Eric Gramond V2.0 07/22/97 
 
Description of Contents:	Contains class Sorting.
				
--------------------------------------------------------------------------*/

/* 
 * Susan H. Rodger, Magda Procopiuc, Octavian Procopiuc, Eric Gramond
 * Computer Science Department
 * Duke University
 * June 1998
 * Supported by National Science Foundation DUE-9596002 and DUE-9555084.
 *
 * Copyright (c) 1998
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the author.  The name of the author may not be used to
 * endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

package flap;


import java.util.*;

/**
  * A sorting class. The only point of having this is to not make the users install
  * JGL if they don't have it.
  */

public abstract class Sorting 
{
  /** the static sorting function */
  static void sort(Vector arr, BinaryPredicate comp) {
    int end = arr.size();
    int smallest;
    Object smallObj;
    for(int i = 0; i<end; i++) {
      smallest = findSmallest(end - i, arr, comp);
      smallObj = arr.elementAt(smallest);
      arr.removeElementAt(smallest);
      arr.addElement(smallObj);
    }
  }


  /**
    * help for the sorter. finds the smallest element within the first
    * limit elements.
    */
  static int findSmallest(int limit, Vector arr, BinaryPredicate comp) {
    int smallest = 0;
    for(int i=1; i < limit; i++) {
      if(!comp.execute(arr.elementAt(smallest), arr.elementAt(i)))
	smallest = i;
    }

    return smallest;
  }
  
}
