通常状况,咱们在Java中给数组排序,比起本身写个冒泡排序,更加喜欢使用Java中自带的sort方法,也就是Arrays.sort
方法数组
可是,这个方法只会将数组从小到大排列,若是咱们须要从大到小排列的数组,怎么办呢?测试
个人想法是,把通过Arrays.sort
方法以后从小到大排列的数组,后面位置的元素与以前的元素进行交换,这样,不就是实现了从大到小的排列了吗?code
须要注意的是:咱们得分两种状况,一种是数组中的元素个数是偶数,另一种则是数组的元素个数为奇数排序
下面则是我实现的方法,通过测试,没有错误方法
/** *传入一个有序的数组a(从小到大排序),返回一个从大到小的数组 * @param a 传入的数组(有序) * @return 返回一个数组(从大到小) */ public static int[] sort(int[] a){ int[] temp = a; if(temp.length%2==0){ //数组里面的个数为偶数 for (int i = 0; i <= temp.length/ 2; i++) { int temp1 = a[i]; temp[i]=temp[temp.length-1-i]; temp[temp.length - 1-i] = temp1; } }else{ //数组里面的个数为奇数 for (int i = 0; i < temp.length / 2; i++) { int temp1 = a[i]; temp[i]=temp[temp.length-1-i]; temp[temp.length - 1-i] = temp1; } } return temp; }