packagelianxi;import java.util.*;public classUnionSearch
{public static voidmain(String[] args)
{int[] array1 = {49,36,13,27,48,100,67,73,85,28,99,56};int[] array2 = {34,45,13,67,23,56,28,90};longmtime,etime,time;
Date myDate= newDate();
mtime=myDate.getTime();//对数组进行快速排序
QuickSort qs = newQuickSort();
array1= qs.quickSort(array1, 0, array1.length - 1);
array2= qs.quickSort(array2, 0, array2.length - 1);//找两数组中的公共元素
int[] array;
array=qs.unionElment(array1, array2);
Date mydate= newDate();
etime=mydate.getTime();
time= etime -mtime;
System.out.println(mtime+" 运行时间为:"+time+" "+etime);//打印公共元素
for(int i = 0; i < array.length;i ++)
{if(array[i] != 0)
{
System.out.print(" ");
System.out.print(array[i]);
}else{break;
}
}
}
}classQuickSort
{public int[] quickSort(int[] array,int low,inthigh)
{if (low >high)
{return null;
}int i =low;int j =high;while (low
{int pivot =array[low];while (low < high && array[high] >=pivot)
{
high--;
}if (low
{int temp =array[low];
array[low]=array[high];
array[high]=temp;
low++;
}while (low < high && array[low] <=pivot)
{
low++;
}if (low
{int temp =array[high];
array[high]=array[low];
array[low]=temp;
high--;
}
}
quickSort(array,i,low- 1);
quickSort(array,low+ 1,j);returnarray;
}public int[] unionElment(int[] array1,int[]array2)
{int length_array1 =array1.length;int length_array2 =array2.length;int union_length = length_array1
{if (array1[i]
{
i++;
}else if (array1[i] >array2[j])
{
j++;
}else{
array[k]=array1[i];
i++;
j++;
k++;
}
}returnarray;
}
}