jpos.util.Sorter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javapos-config-loader Show documentation
Show all versions of javapos-config-loader Show documentation
JavaPOS Configuration Loader Library
package jpos.util;
///////////////////////////////////////////////////////////////////////////////
//
// This software is provided "AS IS". The JavaPOS working group (including
// each of the Corporate members, contributors and individuals) 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 AND
// NON-INFRINGEMENT. The JavaPOS working group shall not be liable for
// any damages suffered as a result of using, modifying or distributing this
// software or its derivatives. Permission to use, copy, modify, and distribute
// the software and its documentation for any purpose is hereby granted.
//
// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which
// is an OSS Apache-like license. The complete license is located at:
// http://www.ibm.com/developerworks/library/os-cpl.html
//
///////////////////////////////////////////////////////////////////////////////
import java.util.*;
/**
* This is utility class to sort Comparable elements in Vector objects
* @author E. Michael Maximilien ([email protected])
* @version 1.1.x (JDK 1.1.x)
*/
public final class Sorter extends Object
{
//-------------------------------------------------------------------------
// Ctor
//
/** Make ctor private to prevent construction */
private Sorter() {}
//-------------------------------------------------------------------------
// Class methods
//
/**
* @return the smallest Comparable element in this Vector
* NOTE:implements a simple one pass algorithm in O(n) time
*/
public static Comparable min( Vector comparables )
{
Comparable smallest = null;
Enumeration elements = comparables.elements();
while( elements.hasMoreElements() )
{
Comparable element = (Comparable)elements.nextElement();
if( smallest != null )
{
if( smallest.compareTo( element ) > 0 )
smallest = element;
}
else
smallest = element;
}
return smallest;
}
/**
* @return the greatest Comparable element in this Vector
* NOTE:implements a simple one pass algorithm in O(n) time
*/
public static Comparable max( Vector comparables )
{
Comparable greatest = null;
Enumeration elements = comparables.elements();
while( elements.hasMoreElements() )
{
Comparable element = (Comparable)elements.nextElement();
if( greatest != null )
{
if( greatest.compareTo( element ) < 0 )
greatest = element;
}
else
greatest = element;
}
return greatest;
}
/**
* @return a Vector of sorted Comparable objects
* NOTE:implements a simple Insertion Sort algorithm
* Sorts n element in place in O(n^2) worst-case time
* @param elements a Vector with the initial Comparable objects
*/
public static Vector insertionSort( Vector comparables )
{
Comparable min = min( comparables );
comparables.removeElement( min );
comparables.insertElementAt( min, 0 );
for( int i = 2; i < comparables.size(); i++ )
{
Comparable temp = (Comparable)comparables.elementAt( i );
int j = 0;
for( j = i; j > 0 && temp.compareTo( (Comparable)comparables.elementAt( j - 1 ) ) < 0; --j )
comparables.setElementAt( comparables.elementAt( j - 1 ), j );
comparables.setElementAt( temp, j );
}
return comparables;
}
/**
* @return a Vector of sorted Comparable objects
* NOTE:implements the Merge Sort algorithm
* Sorts n element in O(nlgn) worst-case time
* @param elements a Vector with the initial Comparable objects
*/
public static Vector mergeSort( Vector comparables )
{
//
throw new RuntimeException( "Not yet implemented!" );
//
}
//-------------------------------------------------------------------------
// Class variables
//
}