- #1
Enharmonics
- 29
- 2
One of my professor's assignments is proving particularly tricky for me. It reads:
"Write the method
public static void sort(String[] a)
that sorts the array a in increasing order. The method must be
a short recursive program. Do not use quicksort or merge sort.
It should be a lot shorter."
The catch: I'm not allowed to change the method header in any way, I'm not allowed to use either merge sort or quick sort (as stated in the instructions), and I'm not allowed to use any extraneous classes, packages, and the like (no java.util.Arrays, etc).
Merge sort and quick sort being disallowed isn't that big a deal for me as I'm not very fond of either, but I'm finding it hard seeing how to do this without passing some auxiliary parameter (to store an index position for the array) that would get used in recursive invocations.
This is what I came up with after a few hours of frustration and basically spinning my wheels:
The basic idea was to check each element "multiple times" with the for loops so as to encourage each element getting into its "proper place" (in the "increasing order" sense). I doubt it's right, though I haven't tested it (professor hasn't uploaded a driver for this assignment yet).
If not, could someone maybe point me in the right direction? There don't seem to be any methods in either the Array or String class that would be useful here. I don't even see the point of using recursion here. Isn't it useless since I'm just passing the method the same array over and over again (to the best of my knowledge there's no method in the Array class to create a "Subarray" of an array, the only thing that comes close is Arrays.asList(), but I'm not allowed to use it)?
"Write the method
public static void sort(String[] a)
that sorts the array a in increasing order. The method must be
a short recursive program. Do not use quicksort or merge sort.
It should be a lot shorter."
The catch: I'm not allowed to change the method header in any way, I'm not allowed to use either merge sort or quick sort (as stated in the instructions), and I'm not allowed to use any extraneous classes, packages, and the like (no java.util.Arrays, etc).
Merge sort and quick sort being disallowed isn't that big a deal for me as I'm not very fond of either, but I'm finding it hard seeing how to do this without passing some auxiliary parameter (to store an index position for the array) that would get used in recursive invocations.
This is what I came up with after a few hours of frustration and basically spinning my wheels:
Java:
public static void sort(String[] a)
{
String temp;
// Note that we use a.length - 1 in the loop condition
// to account for the fact that we will be checking the value of the
// element at the current index against the value of the element at the NEXT index.
// Since array indices are zero-based, iterating from 0 to a.length would result
// in an ArrayIndexOutOfBoundsException when we index = 7, since the
// base case would check a[7] against a[8], the latter of which does not exist
for (int index = 0; index < a.length - 1; index++)
{
if (a[index].length() < a[index + 1].length())
{
continue;
}
else
{
temp = a[index + 1];
a[index + 1] = a[index];
a[index] = temp;
// Recursive call to the sort method
sort(a);
}
}
}
The basic idea was to check each element "multiple times" with the for loops so as to encourage each element getting into its "proper place" (in the "increasing order" sense). I doubt it's right, though I haven't tested it (professor hasn't uploaded a driver for this assignment yet).
If not, could someone maybe point me in the right direction? There don't seem to be any methods in either the Array or String class that would be useful here. I don't even see the point of using recursion here. Isn't it useless since I'm just passing the method the same array over and over again (to the best of my knowledge there's no method in the Array class to create a "Subarray" of an array, the only thing that comes close is Arrays.asList(), but I'm not allowed to use it)?