标签:and 演示 upload lis src com gif oob style
把数据分为两段,从两段中逐个选最小的元素移入新数据段的末尾。
可从上到下或从下到上进行。
过程演示:
一、迭代法
1.待排序数据为data
2.每轮排序的结果保存为result
3.每轮排序的结果result需要重新复制给data
public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> data = new ArrayList<>(); Random random = new Random(42); for (int i = 0; i < 10; i++) { data.add((int)(random.nextInt(100))); } Iterator<Integer> iterator = data.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println(); ArrayList<Integer> result = new ArrayList<>(); result.addAll(data); for (int seg = 1; seg < data.size(); seg=2*seg) { for (int start = 0; start < data.size(); start=start+2*seg) { int start1,end1,start2,end2; int mid = Math.min(start+seg,data.size()); int end = Math.min(start+2*seg,data.size()); start1 = start; end1=mid; start2=mid; end2 = end; int k=start,idx; while(start1<end1 && start2<end2){ // idx = data.get(start1)<data.get(start2)?start1++:start2++; // result.set(k++,data.get(idx)); if (data.get(start1)<data.get(start2)) result.set(k++,data.get(start1++)); else result.set(k++,data.get(start2++)); } while(start1<end1){ result.set(k++,data.get(start1++)); } while(start2<end2){ result.set(k++,data.get(start2++)); } } data.clear(); data.addAll(result); } for (Iterator<Integer> iterator2 = result.iterator(); iterator2.hasNext();) { Integer integer = (Integer) iterator2.next(); System.out.println(integer); } }
标签:and 演示 upload lis src com gif oob style
原文地址:https://www.cnblogs.com/sunupo/p/13235142.html