实现的方法很多,这边给出其中一种,也比较基础的一种,可以尝试加入并发?
package com.wd.bigdata.javaexample.proxy;
import java.util.*;
/**
* Created by taylorchen on 1/7/2017.
*/
public class SortExample {
public static void main(String[] args) {
final int SEED = 50;
final Random random = new Random();
List<List<Integer>> datas = new ArrayList<List<Integer>>();
for(int i=0;i<3;i++){
List<Integer> row = new ArrayList<Integer>();
int elementCount = random.nextInt(SEED);
for(int r = 0 ;r<elementCount;r++){
int t = random.nextInt(SEED);
row.add(t);
}
for(int k=0;k<row.size();k++){
for(int j = k + 1;j<row.size();j++){
if(row.get(k) >row.get(j)){
int t = row.get(k);
row.set(k,row.get(j));
row.set(j,t);
}
}
}
datas.add(row);
}
List<Integer> mergedSortedResult = mergeSort(datas);
System.out.println(mergedSortedResult);
}
public static List<Integer> mergeSort(List<List<Integer>> datas) {
//返回结果
List<Integer> sortedList = new ArrayList<Integer>();
int restRows = datas.size();
//记录二维数据集内当前的下标
Integer[][] map = new Integer[datas.size()][2];
//记录便利完成的子集合
int[] deadRow = new int[datas.size()];
//初始化
for(int e=0;e<datas.size();e++){
map[e][0] = e;
map[e][1] = 0;
}
Integer[] minEntry = null;
while (restRows >= 0) {
minEntry = map[(0 & (datas.size() - 1))];
for (int i = 0; i < datas.size(); i++) {
Integer[] location = map[i];
if(deadRow[minEntry[0]] < 0){
continue;
}
try{
if (datas.get(minEntry[0]).get(minEntry[1]) >
datas.get(location[0]).get(location[1])) {
minEntry = location;
}
}catch (NullPointerException var1){
System.out.println(minEntry);
System.out.println(location);
}
}
sortedList.add(datas.get(minEntry[0]).get(minEntry[1]));
if (minEntry[1] == datas.get(minEntry[0]).size() - 1) {
restRows--;
deadRow[minEntry[0]] = -1;
} else {
map[minEntry[0]][1] = map[minEntry[0]][1] + 1;
}
}
return sortedList;
}
}