java数组——数组排序sort()

例子:

//  排列数组 sort
  int[] array = new int[]{1,4,6,8,3};
  Arrays.sort(array);
  for(int e:array){
   System.out.print(e+",");
  }

运行效果如下:

1,3,4,6,8,

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.

Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.

The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.

The implementation was adapted from Tim Peters's list sort for Python ( TimSort). It uses techiques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, January 1993.

Parameters:
a - the array to be sorted
Throws:
ClassCastException - if the array contains elements that are not mutually comparable (for example, strings and integers)
IllegalArgumentException - (optional) if the natural ordering of the array elements is found to violate the Comparable contract
static voidsort(byte[] a)
Sorts the specified array into ascending numerical order.
static voidsort(byte[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(char[] a)
Sorts the specified array into ascending numerical order.
static voidsort(char[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(double[] a)
Sorts the specified array into ascending numerical order.
static voidsort(double[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(float[] a)
Sorts the specified array into ascending numerical order.
static voidsort(float[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(int[] a)
Sorts the specified array into ascending numerical order.
static voidsort(int[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(long[] a)
Sorts the specified array into ascending numerical order.
static voidsort(long[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static voidsort(Object[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
static voidsort(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.
static voidsort(short[] a)
Sorts the specified array into ascending numerical order.
static voidsort(short[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.
static <T> voidsort(T[] a, Comparator<? super T> c)
Sorts the specified array of objects according to the order induced by the specified comparator.
static <T> voidsort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值