
Sarah P.
asked 08/08/20Solve the question.
Use the 'Selection Sorting Algorithm for Comparable[ ] array' (or any sorting algorithm that can handle Comparable[ ] type array sorting) to arrange words in increasing length order. If same length words, arrange them in alphabetical order.
For example,
abcd, b, cde, ab, abcde, bc --------> b, ab, bc, cde, abcd, abcde
1 Expert Answer

Patrick B. answered 08/08/20
Math and computer tutor/teacher
// Comparable.java
interface Comparable
{
public int Compare ( Object objRec1, Object objRec2);
}
//--------------------------------------------------------------------------------
//StringCompare.java
class StringCompare implements Comparable
{
//returns 1 if strObj1 is longer than strObj2;
// returns -1 if strObj1 is shorter than strObj2;
// if they are the same length, then compares them alphabetically
public int Compare (Object strObj1, Object strObj2)
{
int iReturn;
String str1 = (String) strObj1;
String str2 = (String) strObj2;
int strLen1 = str1.length();
int strLen2 = str2.length();
if (strLen1 != strLen2)
{
iReturn = (strLen1 > strLen2) ? 1 : -1;
}
else
{
iReturn = str1.compareToIgnoreCase(str2);
}
return(iReturn);
}
//*************AmountCompare.java
class AmountCompare implements Comparable
{
// returns 1 if amtObj1>amtObj2, -1 if amtObj1<amtObj2, zer0 otherwise
public int Compare( Object amtObj1, Object amtObj2)
{
int iReturn=0;
double amt1 = ((Double) amtObj1).doubleValue();
double amt2 = ((Double) amtObj2).doubleValue();
if (amt1 != amt2)
{
iReturn = (amt1 > amt2) ? 1 : -1;
}
return(iReturn);
}
}
}
//************** SelectionSort.java
class SelectionSort
{
public void Sort ( Object [] A, Comparable compareCallback)
{
int N= A.length;
int index_of_min=-1;
Object minObjVal = null;
for (int iLoop=0; iLoop<N-1; iLoop++)
{
index_of_min = iLoop;
minObjVal = A[iLoop];
for (int jLoop=iLoop; jLoop<N; jLoop++) //linear searches for the smallest value
{
if (compareCallback.Compare(A[jLoop],minObjVal)<0) // smaller value found
{
index_of_min = jLoop; // updates
minObjVal = A[jLoop];
}
}
//swaps
Object tempObj = A[iLoop];
A[iLoop] = A[index_of_min];
A[index_of_min] = tempObj;
}
}
}
//**************** Main
class MainSort
{
public void Go()
{
String [] strList = {"A","a","ab","ba","b","B","Abc","BAC","bca","cab","cBa","ac","CA","Abcd","BAcd","cbad","dcba","Cbda"};
int N=strList.length;
Object [] objArray = new Object[strList.length];
for (int iLoop=0; iLoop<N; iLoop++)
{
objArray[iLoop] = (Object)strList[iLoop];
}
StringCompare strComp = new StringCompare();
SelectionSort objSort = new SelectionSort();
objSort.Sort(objArray,strComp);
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.println((String)objArray[iLoop]);
}
System.out.println("--------------------------------------------------");
double [] amounts = { 98765.43, 87654.32, 45678.90, 13579.02, 64201.34, 333.33, 35.42, 666.66, 999.99, 101010.10, 24680.13,
97531.24, 52769.77, 76543.21, 42.35, 3.142};
N = amounts.length;
objArray = new Object[N];
for (int iLoop=0; iLoop<N; iLoop++)
{
objArray[iLoop] = (Object) new Double(amounts[iLoop]);
}
AmountCompare amtComp = new AmountCompare();
objSort.Sort(objArray,amtComp);
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.println( ((Double) objArray[iLoop]).doubleValue());
}
}
public static void main( String args[])
{
MainSort x = new MainSort();
x.Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
uploaded to Resources Section. Filename is Java_Selection_Sort.txt, and found under this link. Break them into their respective *.java source code files before re-compiling.08/08/20