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

  • Context: Comp Sci 
  • Thread starter Thread starter apiwowar
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The discussion focuses on implementing a recursive insertion sort for an array of strings in Java. The user attempts to utilize the compareTo method for string comparison but encounters compilation errors due to incorrect indexing with a string variable. Key suggestions include correcting the indexing to use an integer type and simplifying the control flow by removing unnecessary else blocks. The provided code snippet requires adjustments to ensure proper functionality and adherence to Java syntax.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of recursion in algorithms
  • Familiarity with the compareTo method for string comparison
  • Basic knowledge of array manipulation in Java
NEXT STEPS
  • Review Java array indexing and data types
  • Explore recursive algorithms in Java
  • Learn about sorting algorithms, specifically insertion sort
  • Investigate error handling and debugging techniques in Java
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm implementation and debugging in Java.

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
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K