arraysort,arraysort冒泡排序
关于Java中Array.sort()排序原理,越详细越好!
是?Arrays.sort(a);?吧
给你看源码
=============
/*??????*/???public?static?void?sort(int[]?paramArrayOfInt)
/*??????*/???{
/*???96?*/?????sort1(paramArrayOfInt,?0,?paramArrayOfInt.length);
/*??????*/???}
//
/*??????*/???private?static?void?sort1(int[]?paramArrayOfInt,?int?paramInt1,?int?paramInt2)
/*??????*/???{
/*??558?*/?????if?(paramInt2??7)?{
/*??559?*/???????for?(i?=?paramInt1;?i??paramInt2?+?paramInt1;?i++)
/*??560?*/?????????for?(j?=?i;?(j??paramInt1)??(paramArrayOfInt[(j?-?1)]??paramArrayOfInt[j]);?j--)
/*??561?*/???????????swap(paramArrayOfInt,?j,?j?-?1);
/*??562?*/???????return;
/*??????*/?????}
/*??????*/?????
/*??????*/?
/*??566?*/?????int?i?=?paramInt1?+?(paramInt2??1);
/*??567?*/?????if?(paramInt2??7)?{
/*??568?*/???????j?=?paramInt1;
/*??569?*/???????k?=?paramInt1?+?paramInt2?-?1;
/*??570?*/???????if?(paramInt2??40)?{
/*??571?*/?????????m?=?paramInt2?/?8;
/*??572?*/?????????j?=?med3(paramArrayOfInt,?j,?j?+?m,?j?+?2?*?m);
/*??573?*/?????????i?=?med3(paramArrayOfInt,?i?-?m,?i,?i?+?m);
/*??574?*/?????????k?=?med3(paramArrayOfInt,?k?-?2?*?m,?k?-?m,?k);
/*??????*/???????}
/*??576?*/???????i?=?med3(paramArrayOfInt,?j,?i,?k);
/*??????*/?????}
/*??578?*/?????int?j?=?paramArrayOfInt[i];
/*??????*/?????
/*??????*/?
/*??581?*/?????int?k?=?paramInt1;int?m?=?k;int?n?=?paramInt1?+?paramInt2?-?1;int?i1?=?n;
/*??????*/?????for?(;;)?{
/*??583?*/???????if?((m?=?n)??(paramArrayOfInt[m]?=?j))?{
/*??584?*/?????????if?(paramArrayOfInt[m]?==?j)
/*??585?*/???????????swap(paramArrayOfInt,?k++,?m);
/*??586?*/?????????m++;
/*??????*/???????}?else?{
/*??588?*/?????????while?((n?=?m)??(paramArrayOfInt[n]?=?j))?{
/*??589?*/???????????if?(paramArrayOfInt[n]?==?j)
/*??590?*/?????????????swap(paramArrayOfInt,?n,?i1--);
/*??591?*/???????????n--;
/*??????*/?????????}
/*??593?*/?????????if?(m??n)
/*??????*/???????????break;
/*??595?*/?????????swap(paramArrayOfInt,?m++,?n--);
/*??????*/???????}
/*??????*/?????}
/*??????*/?????
/*??599?*/?????int?i3?=?paramInt1?+?paramInt2;
/*??600?*/?????int?i2?=?Math.min(k?-?paramInt1,?m?-?k);vecswap(paramArrayOfInt,?paramInt1,?m?-?i2,?i2);
/*??601?*/?????i2?=?Math.min(i1?-?n,?i3?-?i1?-?1);vecswap(paramArrayOfInt,?m,?i3?-?i2,?i2);
/*??????*/?????
/*??????*/?
/*??604?*/?????if?((i2?=?m?-?k)??1)
/*??605?*/???????sort1(paramArrayOfInt,?paramInt1,?i2);
/*??606?*/?????if?((i2?=?i1?-?n)??1)?{
/*??607?*/???????sort1(paramArrayOfInt,?i3?-?i2,?i2);
/*??????*/?????}
/*??????*/???}
/*??????*/???
/*??????*/?
/*??????*/???private?static?void?swap(int[]?paramArrayOfInt,?int?paramInt1,?int?paramInt2)
/*??????*/???{
/*??614?*/?????int?i?=?paramArrayOfInt[paramInt1];
/*??615?*/?????paramArrayOfInt[paramInt1]?=?paramArrayOfInt[paramInt2];
/*??616?*/?????paramArrayOfInt[paramInt2]?=?i;
/*??????*/???}
JAVA中Arrays.sort()排序的原理是什么?
有的时候需要对数组里的element进行排序。当然可以自己编写合适的排序方法,但既然java包里有自带的Arrays.sort排序方法,在 数组元素比较少的时候为何不用?
Sorting an Array 1. 数字排序? int[] intArray = new int[] { 4, 1, 3, -23 };
Arrays.sort(intArray);
输出: [-23, 1, 3, 4]
2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };
Arrays.sort(strArray);
输出: [C, a, z]
3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
输出: [a, C, z]
4. 反向排序, Reverse-order sort
Arrays.sort(strArray, Collections.reverseOrder());
输出:[z, a, C]
5. 忽略大小写反向排序 Case-insensitive reverse-order sort
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
输出: [z, C, a]
java初学者最常见的错误思想,就是试图去写一些方法来完成数组的排序功能,其实,数组排序功能,在java的api里面早已实现,我们没有 必要去重复制造轮子。
Arrays类有一个静态方法sort,利用这个方法我们可以传入我们要排序的数组进去排序,因为我们传入的是一个数组的引用,所以排序完成的 结果也通过这个引用的来更改数组.对于整数、字符串排序,jdk提供了默认的实现,如果要对一个对象数组排序,则要自己实现 java.util.Comparator接口。
package?com.gjh.gee.arrays;
import?java.util.Arrays;
public?class?ArraySortDemo?{
public?void?sortIntArray()?{
int[]?arrayToSort?=?new?int[]?{?48,?5,?89,?80,?81,?23,?45,?16,?2?};
System.out.println("排序前");
for?(int?i?=?0;?i??arrayToSort.length;?i++)
System.out.println(arrayToSort[i]);
//?调用数组的静态排序方法sort
Arrays.sort(arrayToSort);
System.out.println("排序后");
for?(int?i?=?0;?i??arrayToSort.length;?i++)
System.out.println(arrayToSort[i]);
}
public?void?sortStringArray()?{
String[]?arrayToSort?=?new?String[]?{?"Oscar",?"Charlie",?"Ryan",
"Adam",?"David"?};
System.out.println("排序前");
for?(int?i?=?0;?i??arrayToSort.length;?i++)
System.out.println(arrayToSort[i]);
System.out.println("排序后");
//?调用数组的静态排序方法sort
Arrays.sort(arrayToSort);
for?(int?i?=?0;?i??arrayToSort.length;?i++)
System.out.println(arrayToSort[i]);
}
public?void?sortObjectArray()?{
Dog?o1?=?new?Dog("dog1",?1);
Dog?o2?=?new?Dog("dog2",?4);
Dog?o3?=?new?Dog("dog3",?5);
Dog?o4?=?new?Dog("dog4",?2);
Dog?o5?=?new?Dog("dog5",?3);
Dog[]?dogs?=?new?Dog[]?{?o1,?o2,?o3,?o4,?o5?};
System.out.println("排序前");
for?(int?i?=?0;?i??dogs.length;?i++)?{
Dog?dog?=?dogs[i];
System.out.println(dog.getName());
}
Arrays.sort(dogs,?new?ByWeightComparator());
System.out.println("排序后:");
for?(int?i?=?0;?i??dogs.length;?i++)?{
Dog?dog?=?dogs[i];
System.out.println(dog.getName());
}
}
public?static?void?main(String[]?args)?{
ArraySortDemo?t?=?new?ArraySortDemo();
t.sortIntArray();
t.sortStringArray();
t.sortObjectArray();
}
}
java array.sort用的是什么排序
用Arrays类sort()对数组元素进行降序排列: import java.util.Arrays; class SortDemo { public static void main(String[] args) { int[] vec={0,15,-14,45,20,70}; Arrays.sort(vec); System.out.println("/n"); System.out.println("降序排列