最近在學(xué)習(xí)java多線(xiàn)程,讀了《java多線(xiàn)程編程實(shí)戰(zhàn)》有一種所有的串行操作都可以寫(xiě)成并發(fā)的錯(cuò)覺(jué),所以決定練練手,因?yàn)檫f歸操作最適合并發(fā)操作(除了io等)所以寫(xiě)了一個(gè)并發(fā)版本的歸并排序,并順手?jǐn)]了個(gè)串行的歸并排序,沒(méi)想到在<1000000的數(shù)量級(jí)中竟然比系統(tǒng)提供的排序要快。show you the code
- 首先是生成隨機(jī)數(shù)組(比較亂,也沒(méi)有優(yōu)化,不是重點(diǎn))
class SuijiArray {
private int length = 0;
private int index = 0;
private int[] array = null;
Random rand = new Random();
SuijiArray(int le) {
length = le;
index = le-1;
array = new int[length];
for(int i = 0; i < length; i++) {
array[i] = i+1;
}
}
public int[] get() {
int[] a = new int[length];
for(int i = 0; i< length; i++) {
a[i] = next();
}
return a;
}
private int next() {
if(index == 0){
return array[0];
}
int random = rand.nextInt(index);
int result = array[random];
swap(array,index--,random);
return result;
}
private void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}```
- 然后是一般的歸并排序
public static void insertSort(int[] a,int start, int end) {
for(int i = start + 1; i < end; i++) {
int t = a[i];
int j = i;
for(; j > start && t < a[j-1]; j--) {
a[j] = a[j-1];
}
a[j] = t;
}
}
public static void mergeSort(int[] a, int start, int end){
//在數(shù)組筆記小的時(shí)候,切換到插入排序,提供性能
if((end - start) <= INSERT_SORT_LENGTH ) {
insertSort(a, start, end);
} else {
int mid = (start + end) /2;
mergeSort(a,start,mid);
mergeSort(a, mid, end);
merge(a, start, mid, end);
}
}
public static void merge(int[] a, int start, int mid, int end) {
int[] b= new int[end-start];
int k = 0;
int i = start ;
int j = mid;
while(i < mid && j < end) {
if(a[i] < a[j]) {
b[k++] = a[i++];
}else {
b[k++] = a[j++];
}
}
if(i == mid) {
while(j < end) {
b[k++] = a[j++];
}
}else {
while(i < mid) {
b[k++] = a[i++];
}
}
for(int ii = 0; ii < (end - start);ii++) {
a[start + ii] = b[ii];
}
}```
- 多線(xiàn)程
public class ForkAndJoinTest extends RecursiveTask{
public static final int INSERT_SORT_LENGTH = 23;
private static final long serialVersionUID = 1L;
private int[] array = null;
private int start;
private int end;
ForkAndJoinTest(int[] a, int s, int e) {
array = a;
start = s;
end = e;
}
protected Object compute() {
boolean canCompute = (end - start) <= INSERT_SORT_LENGTH;
if(canCompute){
insertSort(array, start, end);
}else{
int mid = (end + start)/2;
ForkAndJoinTest left = new ForkAndJoinTest(array,start,mid);
ForkAndJoinTest right = new ForkAndJoinTest(array,mid,end);
left.fork();
right.fork();
left.join();
right.join();
merge(array,start,mid,end);
}
return null;
}
}
- 測(cè)試數(shù)據(jù)
public static int[] deepCopy(int[] a) {
int b[] = new int[a.length];
for(int i = 0; i < a.length; i++) {
b[i] = a[i];
}
return b;
}
public static void main(String[] args) {
ForkJoinPool forkJoinPool =new ForkJoinPool(6);
int[] array = new SuijiArray(50000).get();
int[] barray = deepCopy(array);
int[] carray = deepCopy(array);
ForkAndJoinTest test = new ForkAndJoinTest(array,0,array.length);
long end = 0l;
long start = System.currentTimeMillis();
Future future = forkJoinPool.submit(test);
try {
future.get();
end = System.currentTimeMillis();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("concurrentMergeSort:"+(end-start));
long start1 = System.currentTimeMillis();
mergeSort(barray, 0, barray.length);
long end1 = System.currentTimeMillis();
System.out.println("mergeSortTime=:"+(end1 - start1));
long start2 = System.currentTimeMillis();
Arrays.sort(carray);
long end2 = System.currentTimeMillis();
System.out.println("SystemSortTime=:"+(end2 - start2));
}```