From 93670b9d30f2284bcfa3b938b887d283d9354cff Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 7 Feb 2008 19:54:57 +0000 Subject: [PATCH] Operate on List not ArrayList. Widening. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2271 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/QuickSort.java | 181 +++++++++----------- 1 file changed, 80 insertions(+), 101 deletions(-) diff --git a/src/java/com/samskivert/util/QuickSort.java b/src/java/com/samskivert/util/QuickSort.java index 655f02fd..9c83eb86 100644 --- a/src/java/com/samskivert/util/QuickSort.java +++ b/src/java/com/samskivert/util/QuickSort.java @@ -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 void sort (T[] a, Comparator 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 > 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 void rsort (T[] a, Comparator 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 > 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 void sort ( - T[] a, int lo0, int hi0, Comparator comp) + public static void sort (T[] a, int lo0, int hi0, Comparator 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 void rsort ( - T[] a, int lo0, int hi0, Comparator comp) + public static void rsort (T[] a, int lo0, int hi0, Comparator 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 > void sort ( - T[] a, int lo0, int hi0) + public static > 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 > void rsort ( - T[] a, int lo0, int hi0) + public static > 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 > void sort (ArrayList a) + public static > void sort (List a) { sort(a, new Comparator() { 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 void sort (ArrayList a, Comparator comp) + public static void sort (List a, Comparator 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 > void rsort (ArrayList a) + public static > void rsort (List a) { sort(a, new Comparator() { 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 void rsort (ArrayList a, final Comparator comp) + public static void rsort (List a, final Comparator comp) { sort(a, new Comparator() { 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 void sort ( - ArrayList a, int lo0, int hi0, Comparator comp) + public static void sort (List a, int lo0, int hi0, Comparator 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); }