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; package com.samskivert.util;
import java.util.ArrayList; import java.util.List;
import java.util.Comparator; import java.util.Comparator;
/** /**
@@ -29,8 +29,7 @@ import java.util.Comparator;
public class QuickSort public class QuickSort
{ {
/** /**
* Sorts the supplied array of objects from least to greatest, using * Sorts the supplied array of objects from least to greatest, using the supplied comparator.
* the supplied comparator.
*/ */
public static <T> void sort (T[] a, Comparator<? super T> comp) 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 * Sorts the supplied array of comparable objects from least to greatest.
* greatest.
*/ */
public static <T extends Comparable<? super T>> void sort (T[] a) 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 * Sorts the supplied array of objects from greatest to least, using the supplied comparator.
* the supplied comparator.
*/ */
public static <T> void rsort (T[] a, Comparator<? super T> comp) 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 * Sorts the supplied array of comparable objects from greatest to least.
* least.
*/ */
public static <T extends Comparable<? super T>> void rsort (T[] a) 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 * Sorts the specified subset of the supplied array from least to greatest, using the supplied
* greatest, using the supplied comparator. * comparator.
* *
* @param a the array of objects to be sorted. * @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the * @param lo0 the index of the lowest element to be included in the sort.
* sort. * @param hi0 the index of the highest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the * @param comp the comparator to use to establish ordering between elements.
* sort.
* @param comp the comparator to use to establish ordering between
* elements.
*/ */
public static <T> void sort ( public static <T> void sort (T[] a, int lo0, int hi0, Comparator<? super T> comp)
T[] a, int lo0, int hi0, Comparator<? super T> comp)
{ {
// bail out if we're already done // bail out if we're already done
if (hi0 <= lo0) { if (hi0 <= lo0) {
@@ -103,12 +95,12 @@ public class QuickSort
// loop through the array until indices cross // loop through the array until indices cross
for (;;) { for (;;) {
// find the first element that is greater than or equal to // find the first element that is greater than or equal to the partition element
// the partition element starting from the left Index. // starting from the left index
while (comp.compare(a[++lo], mid) < 0); while (comp.compare(a[++lo], mid) < 0);
// find an element that is smaller than or equal to // find an element that is smaller than or equal to the partition element starting from
// the partition element starting from the right Index. // the right index
while (comp.compare(mid, a[--hi]) < 0); while (comp.compare(mid, a[--hi]) < 0);
// swap the two elements or bail out of the loop // 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 // if the right index has not reached the left side of array must now sort the left
// must now sort the left partition // partition
if (lo0 < lo-1) { if (lo0 < lo-1) {
sort(a, lo0, lo-1, comp); sort(a, lo0, lo-1, comp);
} }
// if the left index has not reached the right side of array // if the left index has not reached the right side of array must now sort the right
// must now sort the right partition // partition
if (hi+1 < hi0) { if (hi+1 < hi0) {
sort(a, hi+1, hi0, comp); sort(a, hi+1, hi0, comp);
} }
} }
/** /**
* Sorts the specified subset of the supplied array from greatest to * Sorts the specified subset of the supplied array from greatest to least, using the supplied
* least, using the supplied comparator. * comparator.
* *
* @param a the array of objects to be sorted. * @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the * @param lo0 the index of the lowest element to be included in the sort.
* sort. * @param hi0 the index of the highest element to be included in the sort.
* @param hi0 the index of the highest element to be included in the * @param comp the comparator to use to establish ordering between elements.
* sort.
* @param comp the comparator to use to establish ordering between
* elements.
*/ */
public static <T> void rsort ( public static <T> void rsort (T[] a, int lo0, int hi0, Comparator<? super T> comp)
T[] a, int lo0, int hi0, Comparator<? super T> comp)
{ {
// bail out if we're already done // bail out if we're already done
if (hi0 <= lo0) { if (hi0 <= lo0) {
@@ -171,12 +159,12 @@ public class QuickSort
// loop through the array until indices cross // loop through the array until indices cross
for (;;) { for (;;) {
// find the first element that is greater than or equal to // find the first element that is greater than or equal to the partition element
// the partition element starting from the left Index. // starting from the left index
while (comp.compare(mid, a[++lo]) < 0); while (comp.compare(mid, a[++lo]) < 0);
// find an element that is smaller than or equal to // find an element that is smaller than or equal to the partition element starting from
// the partition element starting from the right Index. // the right index
while (comp.compare(a[--hi], mid) < 0); while (comp.compare(a[--hi], mid) < 0);
// swap the two elements or bail out of the loop // 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 // if the right index has not reached the left side of array must now sort the left
// must now sort the left partition // partition
if (lo0 < lo-1) { if (lo0 < lo-1) {
rsort(a, lo0, lo-1, comp); rsort(a, lo0, lo-1, comp);
} }
// if the left index has not reached the right side of array // if the left index has not reached the right side of array must now sort the right
// must now sort the right partition // partition
if (hi+1 < hi0) { if (hi+1 < hi0) {
rsort(a, hi+1, hi0, comp); rsort(a, hi+1, hi0, comp);
} }
} }
/** /**
* Sorts the specified subset of the supplied array of comparables * Sorts the specified subset of the supplied array of comparables from least to greatest,
* from least to greatest, using the supplied comparator. * using the supplied comparator.
* *
* @param a the array of objects to be sorted. * @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the * @param lo0 the index of the lowest element to be included in the sort.
* sort. * @param hi0 the index of the highest 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 ( public static <T extends Comparable<? super T>> void sort (T[] a, int lo0, int hi0)
T[] a, int lo0, int hi0)
{ {
// bail out if we're already done // bail out if we're already done
if (hi0 <= lo0) { if (hi0 <= lo0) {
@@ -237,12 +222,12 @@ public class QuickSort
// loop through the array until indices cross // loop through the array until indices cross
for (;;) { for (;;) {
// find the first element that is greater than or equal to // find the first element that is greater than or equal to the partition element
// the partition element starting from the left Index. // starting from the left Index.
while (mid.compareTo(a[++lo]) > 0); while (mid.compareTo(a[++lo]) > 0);
// find an element that is smaller than or equal to // find an element that is smaller than or equal to the partition element starting from
// the partition element starting from the right Index. // the right Index.
while (mid.compareTo(a[--hi]) < 0); while (mid.compareTo(a[--hi]) < 0);
// swap the two elements or bail out of the loop // 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 // if the right index has not reached the left side of array must now sort the left
// must now sort the left partition // partition
if (lo0 < lo-1) { if (lo0 < lo-1) {
sort(a, lo0, lo-1); sort(a, lo0, lo-1);
} }
// if the left index has not reached the right side of array // if the left index has not reached the right side of array must now sort the right
// must now sort the right partition // partition
if (hi+1 < hi0) { if (hi+1 < hi0) {
sort(a, hi+1, hi0); sort(a, hi+1, hi0);
} }
} }
/** /**
* Sorts the specified subset of the supplied array of comparables * Sorts the specified subset of the supplied array of comparables from greatest to least,
* from greatest to least, using the supplied comparator. * using the supplied comparator.
* *
* @param a the array of objects to be sorted. * @param a the array of objects to be sorted.
* @param lo0 the index of the lowest element to be included in the * @param lo0 the index of the lowest element to be included in the sort.
* sort. * @param hi0 the index of the highest 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 ( public static <T extends Comparable<? super T>> void rsort (T[] a, int lo0, int hi0)
T[] a, int lo0, int hi0)
{ {
// bail out if we're already done // bail out if we're already done
if (hi0 <= lo0) { if (hi0 <= lo0) {
@@ -303,12 +285,12 @@ public class QuickSort
// loop through the array until indices cross // loop through the array until indices cross
for (;;) { for (;;) {
// find the first element that is greater than or equal to // find the first element that is greater than or equal to the partition element
// the partition element starting from the left Index. // starting from the left index
while (mid.compareTo(a[++lo]) < 0); while (mid.compareTo(a[++lo]) < 0);
// find an element that is smaller than or equal to // find an element that is smaller than or equal to the partition element starting from
// the partition element starting from the right Index. // the right index
while (mid.compareTo(a[--hi]) > 0); while (mid.compareTo(a[--hi]) > 0);
// swap the two elements or bail out of the loop // 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 // if the right index has not reached the left side of array must now sort the left
// must now sort the left partition // partition
if (lo0 < lo-1) { if (lo0 < lo-1) {
rsort(a, lo0, lo-1); rsort(a, lo0, lo-1);
} }
// if the left index has not reached the right side of array // if the left index has not reached the right side of array must now sort the right
// must now sort the right partition // partition
if (hi+1 < hi0) { if (hi+1 < hi0) {
rsort(a, hi+1, hi0); rsort(a, hi+1, hi0);
} }
} }
/** /**
* Sort the elements in the specified ArrayList according to their * Sort the elements in the specified List according to their natural order.
* 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>() { sort(a, new Comparator<T>() {
public int compare (T o1, T o2) { public int compare (T o1, T o2) {
@@ -353,19 +334,18 @@ public class QuickSort
} }
/** /**
* Sort the elements in the specified ArrayList according to the * Sort the elements in the specified List according to the ordering imposed by the specified
* ordering imposed by the specified Comparator. * 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(a, 0, a.size() - 1, comp);
} }
/** /**
* Sort the elements in the specified ArrayList according to their * Sort the elements in the specified List according to their reverse natural order.
* 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>() { sort(a, new Comparator<T>() {
public int compare (T o1, T o2) { public int compare (T o1, T o2) {
@@ -382,10 +362,10 @@ public class QuickSort
} }
/** /**
* Sort the elements in the specified ArrayList according to the * Sort the elements in the specified List according to the reverse ordering imposed by the
* reverse ordering imposed by the specified Comparator. * 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>() { sort(a, new Comparator<T>() {
public int compare (T o1, T o2) { 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 * Sort a subset of the elements in the specified List according to the ordering imposed by the
* to the ordering imposed by the specified Comparator. * specified Comparator.
*/ */
public static <T> void sort ( public static <T> void sort (List<T> a, int lo0, int hi0, Comparator<T> comp)
ArrayList<T> a, int lo0, int hi0, Comparator<T> comp)
{ {
// bail out if we're already done // bail out if we're already done
if (hi0 <= lo0) { if (hi0 <= lo0) {
@@ -428,14 +407,14 @@ public class QuickSort
// loop through the array until indices cross // loop through the array until indices cross
for (;;) { for (;;) {
// find the first element that is greater than or equal to // find the first element that is greater than or equal to the partition element
// the partition element starting from the left Index. // starting from the left index
do { do {
e1 = a.get(++lo); e1 = a.get(++lo);
} while (comp.compare(e1, mid) < 0); } while (comp.compare(e1, mid) < 0);
// find an element that is smaller than or equal to // find an element that is smaller than or equal to the partition element starting from
// the partition element starting from the right Index. // the right index
do { do {
e2 = a.get(--hi); e2 = a.get(--hi);
} while (comp.compare(mid, e2) < 0); } 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 // if the right index has not reached the left side of array must now sort the left
// must now sort the left partition // partition
if (lo0 < lo-1) { if (lo0 < lo-1) {
sort(a, lo0, lo-1, comp); sort(a, lo0, lo-1, comp);
} }
// if the left index has not reached the right side of array // if the left index has not reached the right side of array must now sort the right
// must now sort the right partition // partition
if (hi+1 < hi0) { if (hi+1 < hi0) {
sort(a, hi+1, hi0, comp); sort(a, hi+1, hi0, comp);
} }