Sort an array
[ArraySorter.java] JDK1.1 public class ArraySorter {
  /*
  ** Sort in the same array
  */
  public static void sort(Object[] a, Comparer comparer) {
    sort(a, null, 0, a.length - 1, true, comparer);
  }  /*
  ** Sort a and b, using a as the reference
  */
  public static void sort(Object[] a, Object[] b, 
    int from, int to, boolean ascending, Comparer comparer) {
    // No sort 
    if (a == null || a.length < 2) return;    // sort using Quicksort
    int i = from, j = to;
    Object center = a[ (from + to) / 2 ];
    do {
      if (ascending) {
        while( (i < to) && (comparer.compare(  center, a[i]) > 0) ) 
            i++;
        while( (j > from) && (comparer.compare(center, a[j]) < 0) ) 
            j--;
        } 
      else {
        // Decending sort
        while( (i < to) && (comparer.compare(  center, a[i]) < 0) ) 
           i++;
        while( (j > from) && (comparer.compare(center, a[j]) > 0) ) 
           j--;
        }
      if (i < j) {
        // Swap elements
        Object temp = a[i]; a[i] = a[j]; a[j] = temp;
        // Swap in b array if needed
        if (b != null) {
          temp = b[i]; b[i] = b[j]; b[j] = temp;
        }
      }
      if (i <= j) { i++; j--; }
      } while(i <= j);
    // Sort the rest
    if (from < j) sort(a, b, from, j, ascending, comparer);
    if (i < to) sort(a, b, i, to, ascending, comparer);
    }public static interface Comparer {
  /**
   * The interface implementation should compare the two
   * objects and return an int using these rules:
   * if (a > b)  return > 0;
   * if (a == b) return 0;
   * if (a < b)  return < 0;
   */
   public int compare(Object a, Object b);
   } 
}
 
[testArraySorter.java] public class testArraySorter {
 public static final ASCIIComparer asciiComparer = new ASCIIComparer();
 public static void main(String args[]) {
  if (args.length == 0) 
    System.out.println("give me some args to sort");
  else {
    ArraySorter.sort(args, asciiComparer);
    for (int i = 0; i < args.length; i++) {
      System.out.println(args[i]);
      }
    }
  }public static class ASCIIComparer implements ArraySorter.Comparer {
  public int compare(Object a, Object b) {
   return ((String)a).compareTo((String)b);
   }
  } 

解决方案 »

  1.   

    sort
    public static void sort(long[] a)Sorts the specified array of longs into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(long[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of longs into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(int[] a)Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(int[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(short[] a)Sorts the specified array of shorts into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(short[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of shorts into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(char[] a)Sorts the specified array of chars into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(char[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of chars into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(byte[] a)Sorts the specified array of bytes into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(byte[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of bytes into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(double[] a)Sorts the specified array of doubles into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(double[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of doubles into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(float[] a)Sorts the specified array of floats into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.--------------------------------------------------------------------------------sort
    public static void sort(float[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of floats into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.
    Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length--------------------------------------------------------------------------------sort
    public static void sort(Object[] a)Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.Parameters:
    a - the array to be sorted.
    Throws:
    ClassCastException - if the array contains elements that are not mutually comparable (for example, strings and integers).
    See Also: 
    Comparable--------------------------------------------------------------------------------sort
    public static void sort(Object[] a,
                            int fromIndex,
                            int toIndex)Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in this range must implement the Comparable interface. Furthermore, all elements in this range must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    Throws:
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length
    ClassCastException - if the array contains elements that are not mutually comparable (for example, strings and integers).
    See Also: 
    Comparable--------------------------------------------------------------------------------sort
    public static void sort(Object[] a,
                            Comparator c)Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.Parameters:
    a - the array to be sorted.
    c - the comparator to determine the order of the array.
    Throws:
    ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
    See Also: 
    Comparator--------------------------------------------------------------------------------sort
    public static void sort(Object[] a,
                            int fromIndex,
                            int toIndex,
                            Comparator c)Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.Parameters:
    a - the array to be sorted.
    fromIndex - the index of the first element (inclusive) to be sorted.
    toIndex - the index of the last element (exclusive) to be sorted.
    c - the comparator to determine the order of the array.
    Throws:
    ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
    IllegalArgumentException - if fromIndex > toIndex
    ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length
    See Also: 
    Comparator
      

  2.   

    jdk1.2以后应该用Arrays.sort()比较好的