Comp Sci How Do You Implement Recursive Insertion Sort for Arrays of Strings in Java?

  • Thread starter Thread starter apiwowar
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
To implement recursive insertion sort for an array of strings in Java, the compareTo method should be used for comparisons instead of direct indexing. The current code has a compilation error due to using a string as an index, which is invalid; array indices must be integers. The outer conditional can be simplified by checking if n is greater than 1, eliminating the need for an else block. Additionally, the variable 'j' needs to be initialized before use in the while loop. Proper adjustments to the code structure and logic will help resolve the issues.
apiwowar
Messages
94
Reaction score
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

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);
	
		}
	}
 
Physics news on Phys.org
apiwowar said:
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
I've taken the liberty of adjusting your indentation. You're getting better at it, but it still needed a few tweaks.
apiwowar said:
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);
   }
}

The problem with a[key] is that you are trying to use a string to index into an array - you can't do that. The array index needs to be an integral type, like int or long.

You don't show enough of your code for me to understand what you're trying to do, so I can't offer any alternatives.

One thing you can do to improve your code is to change your outer if - else block. Instead of this...
Code:
if(n <= 1)
{
    //Do nothing, easiest case
}
  
else
{
   ...
}

you can do this...
Code:
if(n > 1)
{
    ...
}
You don't need an else block.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K