apiwowar
- 94
- 0
im having a little trouble getting started with this. it is for an array of strings.
would i do insertion sort the same was as if it was an array of ints or doubles but use the compareTo method to see if i should switch two elements?
this is my attempt so far, it won't compile due to the a[key].
a hint or suggestion would be appreciated. thank you
would i do insertion sort the same was as if it was an array of ints or doubles but use the compareTo method to see if i should switch two elements?
this is my attempt so far, it won't compile due to the a[key].
a hint or suggestion would be appreciated. thank you
Code:
public void insertionSort()
{
insertionSort(a.length-1);
}
private void insertionSort(int n)
{
String temp;
if(n <= 1)
{
//Do nothing, easiest case
}
else
{
for(int i = 1; i < a.length; i++)
{
int j;
String key = a[i];
while((j >= 0) && (a[i].compareTo(a[key]) > 0))
{
a[i+1] = a[i];
j--;
}
a[i+1] = key;
}
insertionSort(n-1);
}
}