Operate on List not ArrayList. Widening.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2271 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2008-02-07 19:54:57 +00:00
parent c96bcb7bce
commit 93670b9d30
+80 -101
View File
@@ -20,7 +20,7 @@
package com.samskivert.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
/**
@@ -29,8 +29,7 @@ import java.util.Comparator;
public class QuickSort
{
/**
* Sorts the supplied array of objects from least to greatest, using
* the supplied comparator.
* Sorts the supplied array of objects from least to greatest, using the supplied comparator.
*/
public static <T> void sort (T[] a, Comparator<? super T> comp)
{
@@ -38,8 +37,7 @@ public class QuickSort
}
/**
* Sorts the supplied array of comparable objects from least to
* greatest.
* Sorts the supplied array of comparable objects from least to greatest.
*/
public static <T extends Comparable<? super T>> void sort (T[] a)
{
@@ -47,8 +45,7 @@ public class QuickSort
}
/**
* Sorts the supplied array of objects from greatest to least, using
* the supplied comparator.
* Sorts the supplied array of objects from greatest to least, using the supplied comparator.
*/
public static <T> void rsort (T[] a, Comparator<? super T> comp)
{
@@ -56,8 +53,7 @@ public class QuickSort
}
/**
* Sorts the supplied array of comparable objects from greatest to
* least.
* Sorts the supplied array of comparable objects from greatest to least.
*/
public static <T extends Comparable<? super T>> void rsort (T[] a)
{
@@ -65,19 +61,15 @@ public class QuickSort
}
/**
* Sorts the specified subset of the supplied array from least to
* greatest, using the supplied comparator.
* Sorts the specified subset of the supplied array from least to greatest, using the supplied
* comparator.
*
* @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the
* sort.
* @param hi0 the index of the highest element to be included in the
* sort.
* @param comp the comparator to use to establish ordering between
* elements.
* @param lo0 the index of the lowest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the sort.
* @param comp the comparator to use to establish ordering between elements.
*/
public static <T> void sort (
T[] a, int lo0, int hi0, Comparator<? super T> comp)
public static <T> void sort (T[] a, int lo0, int hi0, Comparator<? super T> comp)
{
// bail out if we're already done
if (hi0 <= lo0) {
@@ -103,12 +95,12 @@ public class QuickSort
// loop through the array until indices cross
for (;;) {
// find the first element that is greater than or equal to
// the partition element starting from the left Index.
// find the first element that is greater than or equal to the partition element
// starting from the left index
while (comp.compare(a[++lo], mid) < 0);
// find an element that is smaller than or equal to
// the partition element starting from the right Index.
// find an element that is smaller than or equal to the partition element starting from
// the right index
while (comp.compare(mid, a[--hi]) < 0);
// swap the two elements or bail out of the loop
@@ -119,33 +111,29 @@ public class QuickSort
}
}
// if the right index has not reached the left side of array
// must now sort the left partition
// if the right index has not reached the left side of array must now sort the left
// partition
if (lo0 < lo-1) {
sort(a, lo0, lo-1, comp);
}
// if the left index has not reached the right side of array
// must now sort the right partition
// if the left index has not reached the right side of array must now sort the right
// partition
if (hi+1 < hi0) {
sort(a, hi+1, hi0, comp);
}
}
/**
* Sorts the specified subset of the supplied array from greatest to
* least, using the supplied comparator.
* Sorts the specified subset of the supplied array from greatest to least, using the supplied
* comparator.
*
* @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the
* sort.
* @param hi0 the index of the highest element to be included in the
* sort.
* @param comp the comparator to use to establish ordering between
* elements.
* @param lo0 the index of the lowest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the sort.
* @param comp the comparator to use to establish ordering between elements.
*/
public static <T> void rsort (
T[] a, int lo0, int hi0, Comparator<? super T> comp)
public static <T> void rsort (T[] a, int lo0, int hi0, Comparator<? super T> comp)
{
// bail out if we're already done
if (hi0 <= lo0) {
@@ -171,12 +159,12 @@ public class QuickSort
// loop through the array until indices cross
for (;;) {
// find the first element that is greater than or equal to
// the partition element starting from the left Index.
// find the first element that is greater than or equal to the partition element
// starting from the left index
while (comp.compare(mid, a[++lo]) < 0);
// find an element that is smaller than or equal to
// the partition element starting from the right Index.
// find an element that is smaller than or equal to the partition element starting from
// the right index
while (comp.compare(a[--hi], mid) < 0);
// swap the two elements or bail out of the loop
@@ -187,31 +175,28 @@ public class QuickSort
}
}
// if the right index has not reached the left side of array
// must now sort the left partition
// if the right index has not reached the left side of array must now sort the left
// partition
if (lo0 < lo-1) {
rsort(a, lo0, lo-1, comp);
}
// if the left index has not reached the right side of array
// must now sort the right partition
// if the left index has not reached the right side of array must now sort the right
// partition
if (hi+1 < hi0) {
rsort(a, hi+1, hi0, comp);
}
}
/**
* Sorts the specified subset of the supplied array of comparables
* from least to greatest, using the supplied comparator.
* Sorts the specified subset of the supplied array of comparables from least to greatest,
* using the supplied comparator.
*
* @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the
* sort.
* @param hi0 the index of the highest element to be included in the
* sort.
* @param lo0 the index of the lowest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the sort.
*/
public static <T extends Comparable<? super T>> void sort (
T[] a, int lo0, int hi0)
public static <T extends Comparable<? super T>> void sort (T[] a, int lo0, int hi0)
{
// bail out if we're already done
if (hi0 <= lo0) {
@@ -237,12 +222,12 @@ public class QuickSort
// loop through the array until indices cross
for (;;) {
// find the first element that is greater than or equal to
// the partition element starting from the left Index.
// find the first element that is greater than or equal to the partition element
// starting from the left Index.
while (mid.compareTo(a[++lo]) > 0);
// find an element that is smaller than or equal to
// the partition element starting from the right Index.
// find an element that is smaller than or equal to the partition element starting from
// the right Index.
while (mid.compareTo(a[--hi]) < 0);
// swap the two elements or bail out of the loop
@@ -253,31 +238,28 @@ public class QuickSort
}
}
// if the right index has not reached the left side of array
// must now sort the left partition
// if the right index has not reached the left side of array must now sort the left
// partition
if (lo0 < lo-1) {
sort(a, lo0, lo-1);
}
// if the left index has not reached the right side of array
// must now sort the right partition
// if the left index has not reached the right side of array must now sort the right
// partition
if (hi+1 < hi0) {
sort(a, hi+1, hi0);
}
}
/**
* Sorts the specified subset of the supplied array of comparables
* from greatest to least, using the supplied comparator.
* Sorts the specified subset of the supplied array of comparables from greatest to least,
* using the supplied comparator.
*
* @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the
* sort.
* @param hi0 the index of the highest element to be included in the
* sort.
* @param lo0 the index of the lowest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the sort.
*/
public static <T extends Comparable<? super T>> void rsort (
T[] a, int lo0, int hi0)
public static <T extends Comparable<? super T>> void rsort (T[] a, int lo0, int hi0)
{
// bail out if we're already done
if (hi0 <= lo0) {
@@ -303,12 +285,12 @@ public class QuickSort
// loop through the array until indices cross
for (;;) {
// find the first element that is greater than or equal to
// the partition element starting from the left Index.
// find the first element that is greater than or equal to the partition element
// starting from the left index
while (mid.compareTo(a[++lo]) < 0);
// find an element that is smaller than or equal to
// the partition element starting from the right Index.
// find an element that is smaller than or equal to the partition element starting from
// the right index
while (mid.compareTo(a[--hi]) > 0);
// swap the two elements or bail out of the loop
@@ -319,24 +301,23 @@ public class QuickSort
}
}
// if the right index has not reached the left side of array
// must now sort the left partition
// if the right index has not reached the left side of array must now sort the left
// partition
if (lo0 < lo-1) {
rsort(a, lo0, lo-1);
}
// if the left index has not reached the right side of array
// must now sort the right partition
// if the left index has not reached the right side of array must now sort the right
// partition
if (hi+1 < hi0) {
rsort(a, hi+1, hi0);
}
}
/**
* Sort the elements in the specified ArrayList according to their
* natural order.
* Sort the elements in the specified List according to their natural order.
*/
public static <T extends Comparable<? super T>> void sort (ArrayList<T> a)
public static <T extends Comparable<? super T>> void sort (List<T> a)
{
sort(a, new Comparator<T>() {
public int compare (T o1, T o2) {
@@ -353,19 +334,18 @@ public class QuickSort
}
/**
* Sort the elements in the specified ArrayList according to the
* ordering imposed by the specified Comparator.
* Sort the elements in the specified List according to the ordering imposed by the specified
* Comparator.
*/
public static <T> void sort (ArrayList<T> a, Comparator<T> comp)
public static <T> void sort (List<T> a, Comparator<T> comp)
{
sort(a, 0, a.size() - 1, comp);
}
/**
* Sort the elements in the specified ArrayList according to their
* reverse natural order.
* Sort the elements in the specified List according to their reverse natural order.
*/
public static <T extends Comparable<? super T>> void rsort (ArrayList<T> a)
public static <T extends Comparable<? super T>> void rsort (List<T> a)
{
sort(a, new Comparator<T>() {
public int compare (T o1, T o2) {
@@ -382,10 +362,10 @@ public class QuickSort
}
/**
* Sort the elements in the specified ArrayList according to the
* reverse ordering imposed by the specified Comparator.
* Sort the elements in the specified List according to the reverse ordering imposed by the
* specified Comparator.
*/
public static <T> void rsort (ArrayList<T> a, final Comparator<T> comp)
public static <T> void rsort (List<T> a, final Comparator<T> comp)
{
sort(a, new Comparator<T>() {
public int compare (T o1, T o2) {
@@ -395,11 +375,10 @@ public class QuickSort
}
/**
* Sort a subset of the elements in the specified ArrayList according
* to the ordering imposed by the specified Comparator.
* Sort a subset of the elements in the specified List according to the ordering imposed by the
* specified Comparator.
*/
public static <T> void sort (
ArrayList<T> a, int lo0, int hi0, Comparator<T> comp)
public static <T> void sort (List<T> a, int lo0, int hi0, Comparator<T> comp)
{
// bail out if we're already done
if (hi0 <= lo0) {
@@ -428,14 +407,14 @@ public class QuickSort
// loop through the array until indices cross
for (;;) {
// find the first element that is greater than or equal to
// the partition element starting from the left Index.
// find the first element that is greater than or equal to the partition element
// starting from the left index
do {
e1 = a.get(++lo);
} while (comp.compare(e1, mid) < 0);
// find an element that is smaller than or equal to
// the partition element starting from the right Index.
// find an element that is smaller than or equal to the partition element starting from
// the right index
do {
e2 = a.get(--hi);
} while (comp.compare(mid, e2) < 0);
@@ -449,14 +428,14 @@ public class QuickSort
}
}
// if the right index has not reached the left side of array
// must now sort the left partition
// if the right index has not reached the left side of array must now sort the left
// partition
if (lo0 < lo-1) {
sort(a, lo0, lo-1, comp);
}
// if the left index has not reached the right side of array
// must now sort the right partition
// if the left index has not reached the right side of array must now sort the right
// partition
if (hi+1 < hi0) {
sort(a, hi+1, hi0, comp);
}