1) Create an ObjectQuickSorter class that can sort Comparable objects. 2) Create
ID: 3567454 • Letter: 1
Question
1) Create an ObjectQuickSorter class that can sort Comparable objects.
2) Create an ObjectBinarySearcher class that can search an array of Comparable objects.
3) In Problem 2 you wrote an ObjectBinarySearcher class. Modify the search method so it is a generic method. The method should use generic notation to accept an array of any objects that implement the Comparable interface. Since Binary search works on a sorted list, modify your ObjectQuickSorter class to be a generic class.
4) Write a generic class with a type parameter constrained to any type that implements Comparable. The constructor should accept an array of such objects. The class should have methods that return the highest and lowest values in the array also please include toString method.
Explanation / Answer
1. Program:
Public class ObjectQuickSorter
{
Private static void doQuickSort (comparable[] array, start, end)
{
String pivotPoint;
If(start<end)
{
pivotPoint=partition(array, start, end);
doQuickSort(array, start, pivotPoint-1);
doQuickSort(array, pivotPoint+1, end);
}
}
Private static void partition (comparable[] array, start, end)
{
String pivotValue;
String endOfLeftList;
String mid;
Mid=(start+End)/2;
Swap(array, start, mid);
pivotValue=array[start];
endOfLeftList=start;
for(scan=0; scan<lastPos-1; scan++)
{
For(scan=start+1; scan<=eend; scan++)
{
If(array[scan].compareTo(pivotValue)<0)
{
endOfLeftList++;
swap(array, endOfLeftList, scan);
}
}
Swap(array, start, endOfLeftList);
Return endOfLeftList;
}
Private static void swap (Comparable[] array, a, B)/>
{
String temp;
Temp=array[a];
Array[a]=array[b];
Array[b]=temp;
}
2. Program:
{
/**
* called from other program to find an element index in an array
*/
public static int binSearchAll(T find, T array[])
{
return binarySearch(array, 0, (array.length)-1, find);
}
public static int binarySearch(T array[], int lower, int upper, T X)
//x is the element to find
{
if (upper < lower)
return -1;
int middle = (lower + upper) / 2;
if (array[middle].compareTo(X))
return middle;
if (array[middle] < X)
return binarySearch(a, middle+1, upper, X);
else
return binarySearch(a, lower, middle-1, X);
}
}
3. Program:
4. Program :
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.