![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/82dd6c762bb1bff46e82b3c786aff77b.png)
类图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0e3bac8249ce810fc144f98545b4cf6a.png)
代码如下
Client客户端
```handlebars
public class Client {
public static void main(String args[])
{
DataOperation operation;
QuickSort sort=new QuickSort();
BinarySearch search=new BinarySearch();
operation =new OperationAdapter(sort,search);
int array[]={13,24,15,36,26,17,68,34};
int result[];
int value;
System.out.println("排序结果:");
result=operation.sort(array);
for (int i:array)
{
System.out.println(i+",");
}
System.out.println();
System.out.println("查找关键字24:");
value=operation.search(result,24);
if(value!=-1){
System.out.println("找到关键字24.");
}
else
{
System.out.println("没有找到关键字24.");
}
}
}
BinarySearch查找
public class BinarySearch {
public int binarySearch(int array[] ,int key)
{
int low=0;
int high=array.length-1;
while (low<=high)
{
int mid=(low+high)/2;
int midVal=array[mid];
if(midVal<key)
{
low=mid+1;
}
else if(midVal>key)
{
high=mid-1;
}else
{
return 1;
}
}
return -1;
}
}
QuickSort排序
public class QuickSort {
public int[] quickSort(int array[]){
sort(array,0,array.length-1);
return array;
}
public void sort (int array[] ,int p ,int r){
int q=0;
if(p<r)
{
q=partition(array,p,r);
sort(array,p,q-1);
sort(array,q+1,r);
}
}
public int partition(int[] a,int p,int r){
int x=a[r];
int j=p-1;
for(int i=p;i<=r-1;i++)
{
if(a[i]<=x)
{
j++;
swap(a,j,i);
}
}
swap(a,j+1,r);
return j+1;
}
public void swap(int[] a,int i,int j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
DataOperation接口
public interface DataOperation {
public int[] sort(int array[]);
public int search(int array[],int key);
}
OperationAdapter类
public class OperationAdapter implements DataOperation{
private QuickSort sort;
private BinarySearch search;
public OperationAdapter(QuickSort sort, BinarySearch search) {
this.sort = sort;
this.search = search;
}
public QuickSort getSort() {
return sort;
}
public void setSort(QuickSort sort) {
this.sort = sort;
}
public BinarySearch getSearch() {
return search;
}
public void setSearch(BinarySearch search) {
this.search = search;
}
@Override
public int[] sort(int[] array) {
return sort.quickSort(array);
}
@Override
public int search(int[] array, int key) {
return search.binarySearch(array,key);
}
}