有人可以帮助我完成我的递归代码吗?这是我理解它的方式(但我认为我没有正确地介绍代码):
if ( first > last ) return -1
if ( result == 0 ) return last
else return SeqSearch (data, first, last-1, key)
last
为last-1
(“keller”)else return SeqSearch(data, first, last-1, key)
last
为last-1
(“6”)这是我的代码:
public static void main (String[] args)
{
String[] data = new String[]{"help","jackson","six","keller","mean"};
int first = 0;
int last = data.length-1;
String key ="help";
System.out.println(SeqSearch(data,first,last,key));
}
public static int SeqSearch(String[] data,int first,int last,String key)
{
if(first > last)
return -1;
else{
int result = data[last].compareTo(key);
if(result == 0)
return last;
else
return SeqSearch(data,first,last-1,key);
}
}