先码后看 Collection的sort方法 侵立删
来源:互联网 发布:建筑施工图设计软件 编辑:程序博客网 时间:2024/06/06 02:14
1.collections转化为array,并借助于arrays的sort功能完成排序,并回写到collectionvoid mergerSort(int[] a){int len = a.lenght()int mid = len>>2if(len>1){int[] pre=a[0:mid);int[] after=a[mid:len);mergerSort(pre);mergerSort(after);merge(a,pre,after)}}
2. Arrays合并排序的实现:public static <T> void sort(List<T> list, Comparator<? super T> c) { Object[] a = list.toArray(); Arrays.sort(a, (Comparator)c); ListIterator i = list.listIterator(); for (int j=0; j<a.length; j++) { i.next(); i.set(a[j]); } }
public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } /** To be removed in a future release. */ private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { T[] aux = a.clone(); if (c==null) mergeSort(aux, a, 0, a.length, 0); else mergeSort(aux, a, 0, a.length, 0, c); }
private static void mergeSort(Object[] src, Object[] dest, int low, int high, int off) { int length = high - low; // Insertion sort on smallest arrays if (length < INSERTIONSORT_THRESHOLD) { for (int i=low; i<high; i++) for (int j=i; j>low && ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) swap(dest, j, j-1); return; } // Recursively sort halves of dest into src int destLow = low; int destHigh = high; low += off; high += off; int mid = (low + high) >>> 1; mergeSort(dest, src, low, mid, -off); mergeSort(dest, src, mid, high, -off); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; } // Merge sorted halves (now in src) into dest for(int i = destLow, p = low, q = mid; i < destHigh; i++) { if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) dest[i] = src[p++]; else dest[i] = src[q++]; } }
List中含有4个元素,根据合并排序的算法,首先分为[0:2) 和[2:4)public class TestCompare {private String com;private int id;public TestCompare(int id, String com) {super();this.com = com;this.id = id;}@Overridepublic String toString() {return "TestCompare [com=" + com + ", id=" + id + "]";}/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubList<TestCompare> li = new ArrayList<TestCompare>();li.add(new TestCompare(1, null));li.add(new TestCompare(2, "dfsd"));li.add(new TestCompare(3, null));li.add(new TestCompare(4, "ying"));Collections.sort(li, new Comparator<TestCompare>() {@Overridepublic int compare(TestCompare o1, TestCompare o2) {// TODO Auto-generated method stubif (o1.com == o2.com)return 0;else if (o1.com == null)return 1;else if (o2.com == null)return -1;elsereturn o1.com.compareTo(o2.com);}});
- 先码后看 Collection的sort方法 侵立删
- java-容器-collection的sort方法
- java-容器-collection的sort方法
- collection.sort()方法详解
- list 的自定义排序 Collection.sort()方法个人学习总结
- Collection.sort
- Java 数序和集合框架的转化 & Collection.sort()方法排序的两种方式
- Java集合对象的排序使用Collection.sort方法,先实现Comparable接口
- 关于Collection.sort和java的枚举
- Collection.sort 的用法--很有用哦
- Collection类的shuffle()、sort()、binarySearch()
- Collection.sort自定义排序的使用方法
- 用collection.sort()方法对list集合排序
- 重写Collection.sort方法时,判断返回值作用
- 关于Collection.sort
- collection.sort排序
- PLSQL Collection Sort
- java Collection.sort
- SpringMVC导出Excel
- Python读取excel指定列生成指定sql脚本
- 用Beautiful Soup进行屏幕抓取
- DOM扩展:HTML5
- JSONObject和JSONArray的操作
- 先码后看 Collection的sort方法 侵立删
- 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)
- Test
- Git详解之六 Git工具
- 【Git】安装以及第一次使用Git和GitHub傻瓜教程
- 使用Myeclipse10建立WebService工程的配置详解(附例程)
- fastjson解释json和object问题
- netty demo学习文章汇总
- 下拉框问题