现有一个接口DataOperation定义了排序方法sort(int[]) 和查找方法search(int[], int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法,类BinarySearch 的binarySearch(int[], int)方法实现了二分查找算法。现使用适配器模式设计一个系统,在不修改源代码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。绘制类图并编程实现。(要求实现快速排序和二分查找)
package AdapterMode;
public class QuickSort {
public void quickSort(int[]a,int first, int end){
if(first<end){
int pivot = partition(a,first,end);
quickSort(a,first,pivot-1);
quickSort(a,pivot+1,end);
}
}
public int partition(int[] a , int first , int end){
int i = first; int j = end;
while(i<j){
while(i<j&&a[i]<=a[j])j--;
if(i<j){
int temp;
temp = a[i];
a[i]=a[j];
a[j]= temp;
i++;
}
while(i<j&&a[i]<=a[j])i++;
if(i<j){
int temp;
temp = a[j];
a[j]=a[i];
a[i]= temp;
j--;
}
}
return i ;
}
}
package AdapterMode;
public class BinarySearch {
public int binarySearch(int srcArray[],int key){
int mid = srcArray.length / 2;
if (key == srcArray[mid])
{
return mid;
}
int start = 0;
int end = srcArray.length - 1;
while (start <= end)
{
mid = (end - start) / 2 + start;
if (key < srcArray[mid])
{
end = mid - 1;
}
else if (key > srcArray[mid])
{
start = mid + 1;
} else
{
return mid;
}
}
return -1;
}
}
package AdapterMode;
public interface DataOperation {
public void sort(int r[]);
public int search(int r[],int n);
}