国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

JAVA實現歸并排序

2019-11-06 06:25:17
字體:
來源:轉載
供稿:網友
package tree;import java.util.ArrayList;import java.util.List;/** * Created by rightHead on 2017/3/6. */public abstract class MergeSort<T> { PRivate T[] result; public abstract boolean compare(T arg1, T arg2); public void sort(T[] s, int start, int end) { result = s; Sort(start, end); } private void Sort(int start, int end) { if (start >= end){ return; } T[] cache = result; List<T> group1 = new ArrayList<>(); List<T> group2 = new ArrayList<>(); int group1Start, group1End, group2Start, group2End; if (end - start == 1){ group1Start = group1End = start; group2Start = group2End = end; }else{ int temp = (start+end) >> 1; group1Start = start; group1End = temp; group2Start = temp+1; group2End = end; } for (int i=group1Start; i<=group1End; i++){ group1.add(cache[i]); } Sort(group1Start, group1End); group1.removeAll(group1); // 這個方法的效率未知 for (int i=group1Start; i<=group1End; i++){ group1.add(cache[i]); } for (int i=group2Start; i<=group2End; i++){ group2.add(cache[i]); } Sort(group2Start, group2End); group2.removeAll(group2); for (int i=group2Start; i<=group2End; i++){ group2.add(cache[i]); } int i=0,j=0,k=start; int group1Size = group1.size(); int group2Size = group2.size(); while (true){ if (compare(group1.get(i), group2.get(j))){ cache[k++] = group1.get(i++); }else{ cache[k++] = group2.get(j++); } if (i == group1Size){ while (j != group2Size){ cache[k++] = group2.get(j++); } return; } if (j == group2Size){ while (i != group1Size){ cache[k++] = group1.get(i++); } return; } } }} public static void main(String[] args) { MergeSort<Integer> mergeSort = new MergeSort<Integer>() { @Override public boolean compare(Integer arg1, Integer arg2) { return arg1 > arg2; } }; Integer[] xo = {8,4,7,5,9,2,1,3,0}; mergeSort.sort(xo, 0, xo.length-1); for (int s : xo){ System.out.println(s); } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 内江市| 阜宁县| 南乐县| 昭平县| 南川市| 库车县| 九寨沟县| 木兰县| 哈巴河县| 曲松县| 武强县| 大丰市| 万宁市| 巨鹿县| 平南县| 邵阳县| 朝阳县| 武功县| 中江县| 汝阳县| 太保市| 衡阳县| 凉山| 荆州市| 宁津县| 南通市| 宜昌市| 西充县| 桂林市| 开江县| 南充市| 定远县| 嵊州市| 淳安县| 宣威市| 山东省| 遵义县| 汾阳市| 祁阳县| 大余县| 潮州市|