Class Sorter

Direct Known Subclasses:
BinarySearch, BubbleSort, Mergesort, Quicksort, SelectionSort, SequentialSearch

public class Sorter

Class for performing "instrumented" sorts and searches (where we count how many comparisons and swaps were needed). This class provides convenience methods; subclasses implement various sort/search algorithms. (See source code at Sorter.java.)


Inner Class Summary
 public static classSorter.SearchResult
          Class to hold counter and "found" values resulting from a search.
 public static classSorter.SortResult
          Class to hold counter values resulting from a sort.

Method Summary
 static booleanisSorted(Object[] a, Comparator c)
          Tests whether array is sorted.
 static voidprintArray(String msg, Object[] a)
          Prints array, preceded by header message.
 static voidprintSearchResult(String testName, String searchName, Object[] a, Object x, SearchResult r)
          Shows results of testing search: Tests result and prints values of counters, preceded by header message.
 static voidprintSortResult(String testName, String sortName, Object[] a, Comparator c, SortResult r)
          Shows results of testing sort: Tests result and prints values of counters, preceded by header message.
 static voidprintSortResult(String testName, String sortName, Object[] a, Comparator c, SortResult r, boolean copyNotSwap)
          Shows results of testing sort: Tests result and prints values of counters, preceded by header message.

Method Detail

isSorted

public static boolean isSorted(Object[] a, Comparator c)
Tests whether array is sorted.
Parameters:
a array
c comparator, or null to use arrays's compareTo method (in which case a should be Comparable)
Returns: true if elements of a are in non-descending order, false otherwise

printArray

public static void printArray(String msg, Object[] a)
Prints array, preceded by header message.
Parameters:
msg header message
a array

printSearchResult

public static void printSearchResult(String testName, String searchName, Object[] a, Object x, SearchResult r)
Shows results of testing search: Tests result and prints values of counters, preceded by header message.
Parameters:
testName "test name" for header message
searchName "sort name" for header message
a array being searched
x element being searched for
r result of performing search

printSortResult

public static void printSortResult(String testName, String sortName, Object[] a, Comparator c, SortResult r)
Shows results of testing sort: Tests result and prints values of counters, preceded by header message.
Parameters:
testName "test name" for header message
sortName "sort name" for header message
a array (presumably sorted)
c comparator, or null to use compareTo
r result of performing sort

printSortResult

public static void printSortResult(String testName, String sortName, Object[] a, Comparator c, SortResult r, boolean copyNotSwap)
Shows results of testing sort: Tests result and prints values of counters, preceded by header message.
Parameters:
testName "test name" for header message
sortName "sort name" for header message
a array (presumably sorted)
c comparator, or null to use compareTo
r result of performing sort
copyNotSwap whether sort performed copies (true) or swaps (false)