JAVA——归并算法 Fork/Join框架

来源: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;
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值