Class Sorter

java.lang.Object
  extended bySorter

public class Sorter
extends java.lang.Object

Class Sorter provides sorting methods on linear arrays of double. Methods sortSelection(), sortInsertion(), sortBubble() and sortMerge() sort the array that is passed as an argument to them. Class Sorter uses the private methods minLocationFrom() and swap() to facilitate the development of the sorting methods. It provides the public method merge() which merges two sorted arrays. It also provides a random number generator which fills an array and array printing methods.

Version:
(11 March 2004)
Author:
(Antonios Symvonis)

Constructor Summary
Sorter()
           
 
Method Summary
static double[] clone(double[] original)
          Creates an exact copy of an array.
static double[] extract(double[] original, int start, int end)
          Extracts a portion of an array.
static void fillRandomData(double[] a)
          Fills a linear array with radmon data in the range [0..1).
static double[] merge(double[] a1, double[] a2)
          Merges two sorted arrays.
static void printH(double[] a)
          Prints the elements of an array, all at the same line.
static void printV(double[] a)
          Prints the elements of an array, each at a different line.
static void sortBubble(double[] a)
          Sorts an array in O(n^2) time by using the "Bubble-sort" method.
static void sortInsertion(double[] a)
          Sorts an array in O(n^2) time by using the "insertion-sort" method.
static void sortMerge(double[] a)
          Sorts an array in O(nlogn) time by using the "merge-sort" method.
static void sortSelection(double[] a)
          Sorts an array in O(n^2) time by using the "selection-sort" method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sorter

public Sorter()
Method Detail

clone

public static double[] clone(double[] original)
Creates an exact copy of an array.

Returns:
An exact copy of the argument array.

extract

public static double[] extract(double[] original,
                               int start,
                               int end)
Extracts a portion of an array.

Parameters:
start - The index of the first element to be extracted.
end - The index of the last element to be extracted. It holds that (start <= end).
Returns:
The extracted array.

fillRandomData

public static void fillRandomData(double[] a)
Fills a linear array with radmon data in the range [0..1). The random numbers have precision of 2 decimal digits.

Parameters:
a - The array to be filled. It cannot be NULL.

merge

public static double[] merge(double[] a1,
                             double[] a2)
Merges two sorted arrays.

Parameters:
a1 - The first of the two arrays to be merged. It must be sorted and cannot be NULL.
a2 - The second of the two arrays to be merged. It must be sorted and cannot be NULL.
Returns:
The merged array.

printH

public static void printH(double[] a)
Prints the elements of an array, all at the same line.

Parameters:
a - The array of which the ellements are printed.

printV

public static void printV(double[] a)
Prints the elements of an array, each at a different line.

Parameters:
a - The array of which the ellements are printed.

sortBubble

public static void sortBubble(double[] a)
Sorts an array in O(n^2) time by using the "Bubble-sort" method.

Parameters:
a - The array to be sorted. It cannot be NULL.

sortInsertion

public static void sortInsertion(double[] a)
Sorts an array in O(n^2) time by using the "insertion-sort" method.

Parameters:
a - The array to be sorted. It cannot be NULL.

sortMerge

public static void sortMerge(double[] a)
Sorts an array in O(nlogn) time by using the "merge-sort" method. This implementation is not in place, that is, it uses extra additional space.

Parameters:
a - The array to be sorted. It cannot be NULL.

sortSelection

public static void sortSelection(double[] a)
Sorts an array in O(n^2) time by using the "selection-sort" method.

Parameters:
a - The array to be sorted. It cannot be NULL.