来源:http://blog.youkuaiyun.com/yinwenjie
简单的归并算法实现
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
public class MergeSort {
private static int MAX=10000;
private static int inits[]=new int[MAX];
//生成随机整数集合
static {
Random r=new Random();
for(int index=0;index<MAX;index++)
{
inits[index]=r.nextInt(10000000);
}
}
public static void main(String[] args)throws Exception {
long beginTime=System.currentTimeMillis();
int results[]=forkits(inits);
long endTime=System.currentTimeMillis();
System.out.println("耗时=" + (endTime - beginTime) + " | " + Arrays.toString(results));
}
//拆分
private static int[] forkits(int source[]) {
int sourceLen=source.length;
if(sourceLen>2)
{
int midIndex=sourceLen/2;
int result1[]=forkits(Arrays.copyOf(source,midIndex));//from zero
int result2[]=forkits(Arrays.copyOfRange(source, midIndex, sourceLen));
int final_res[]=joinInts(result1,result2);
return final_res;
}
else
{
if(sourceLen==1||source[0]<source[1])
{
return source;
}
else
{
int temp[]=new int[2];
temp[0]=source[1];
temp[1]=source[0];
return temp;
}
}
}
//合并
private static int[] joinInts(int array1[],int array2[]) {
int array1Len=array1.length;
int array2Len=array2.length;
int joinedArrayLen=array1Len+array2Len;
int joinedArray[]=new int[joinedArrayLen];
for(int joinedIndex=0,array1Index=0,array2Index=0;joinedIndex<joinedArrayLen;joinedIndex++)
{
int value1=array1Index>=array1Len?Integer.MAX_VALUE:array1[array1Index];
int value2=array2Index>=array2Len?Integer.MAX_VALUE:array2[array2Index];
if(value1<value2)
{
joinedArray[joinedIndex]=value1;
array1Index++;
}
else
{
joinedArray[joinedIndex]=value2;
array2Index++;
}
}
return joinedArray;
}
}
Fork/Join框架下的归并算法实现
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
public class MergeSort2 {
private static int MAX=1000000;
private static int inits[]=new int[MAX];
static {
Random r=new Random();
for(int index=0;index<MAX;index++)
{
inits[index]=r.nextInt(10000000);
}
}
public static void main(String[] args)throws Exception {
long beginTime = System.currentTimeMillis();
ForkJoinPool pool= new ForkJoinPool();
MyTask task=new MyTask(inits);
ForkJoinTask<int[]> taskResult=pool.submit(task);
try {
taskResult.get();
} catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace(System.out);
}
long endTime = System.currentTimeMillis();
System.out.println("耗时=" + (endTime - beginTime));
}
static class MyTask extends RecursiveTask<int[]>{
private int source[];
public MyTask(int source[]) {
this.source=source;
}
@Override
protected int[] compute() {
int sourceLen=source.length;
if(sourceLen>2)
{
int midIndex=sourceLen/2;
MyTask task1=new MyTask(Arrays.copyOf(source, midIndex));
task1.fork();
MyTask task2=new MyTask(Arrays.copyOfRange(source,midIndex,sourceLen));
task2.fork();
int result1[]=task1.join();
int result2[]=task2.join();
int final_res[]=joinInts(result1,result2);
return final_res;
}
else {
if(sourceLen==1||source[0]<source[1])
{
return source;
}
else
{
int temp[]=new int[2];
temp[0]=source[1];
temp[1]=source[0];
return temp;
}
}
}
private int[] joinInts(int array1[],int array2[]) {
int array1Len=array1.length;
int array2Len=array2.length;
int joinedArrayLen=array1Len+array2Len;
int joinedArray[]=new int[joinedArrayLen];
for(int joinedIndex=0,array1Index=0,array2Index=0;joinedIndex<joinedArrayLen;joinedIndex++)
{
int value1=array1Index>=array1Len?Integer.MAX_VALUE:array1[array1Index];
int value2=array2Index>=array2Len?Integer.MAX_VALUE:array2[array2Index];
if(value1<value2)
{
joinedArray[joinedIndex]=value1;
array1Index++;
}
else
{
joinedArray[joinedIndex]=value2;
array2Index++;
}
}
return joinedArray;
}
}
}