A class for doing quick sorts.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@580 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// $Id: QuickSortTest.java,v 1.1 2002/02/19 03:37:32 mdb Exp $
|
||||
|
||||
package com.samskivert.util;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests the {@link QuickSort} class.
|
||||
*/
|
||||
public class QuickSortTest extends TestCase
|
||||
{
|
||||
public QuickSortTest ()
|
||||
{
|
||||
super(QuickSortTest.class.getName());
|
||||
}
|
||||
|
||||
public void runTest ()
|
||||
{
|
||||
Integer[] a = new Integer[100];
|
||||
|
||||
Comparator comp = new Comparator() {
|
||||
public int compare (Object x, Object y) {
|
||||
return ((Integer)x).intValue() - ((Integer)y).intValue();
|
||||
}
|
||||
};
|
||||
|
||||
for (int d = 1; d <= 100; d++) {
|
||||
for (int n = 0; n < 100; n++) {
|
||||
a[n] = new Integer(n / d);
|
||||
QuickSort.csort (a, 0, n, comp);
|
||||
for (int i = 0; i <= n; i++) {
|
||||
assert("Failure for up " + n + "/" + d,
|
||||
a[i].intValue() == i / d);
|
||||
}
|
||||
}
|
||||
}
|
||||
// System.out.println("up test ok");
|
||||
|
||||
for (int d = 1; d <= 100; d++) {
|
||||
for (int n = 0; n < 100; n++) {
|
||||
for (int i = 0; i <= n; i++) {
|
||||
a[i] = new Integer((n - i) / d);
|
||||
}
|
||||
QuickSort.csort (a, 0, n, comp);
|
||||
for (int i = 0; i <= n; i++) {
|
||||
assert("Failure for down " + n + "/" + d,
|
||||
a[i].intValue() == i / d);
|
||||
}
|
||||
}
|
||||
}
|
||||
// System.out.println("down test ok");
|
||||
|
||||
int tests = 1000;
|
||||
for (int sorts = 0; sorts < tests; sorts++) {
|
||||
int n = rand(100);
|
||||
for (int i = 0; i <= n; i++) {
|
||||
a[i] = new Integer(rand(30000));
|
||||
}
|
||||
|
||||
QuickSort.csort (a, 0, n, comp);
|
||||
for (int i = 0; i < n; i++) {
|
||||
assert("Failure for random " + n,
|
||||
a[i].intValue() <= a[i+1].intValue());
|
||||
}
|
||||
|
||||
QuickSort.csort (a, 0, n, comp);
|
||||
for (int i = 0; i < n; i++) {
|
||||
assert("Failure for random " + n + " (resort)",
|
||||
a[i].intValue() <= a[i+1].intValue());
|
||||
}
|
||||
|
||||
a[rand(n+1)] = new Integer(rand(30000));
|
||||
QuickSort.csort (a, 0, n, comp);
|
||||
for (int i = 0; i < n; i++) {
|
||||
assert("Failure for random " + n + " (resort 2)",
|
||||
a[i].intValue() <= a[i+1].intValue());
|
||||
}
|
||||
}
|
||||
// System.out.println("successfully sorted " + tests +
|
||||
// " random arrays");
|
||||
}
|
||||
|
||||
public static Test suite ()
|
||||
{
|
||||
return new QuickSortTest();
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
QuickSortTest test = new QuickSortTest();
|
||||
test.runTest();
|
||||
}
|
||||
|
||||
private static int rand (int n)
|
||||
{
|
||||
return (int)(Math.random() * n);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user