• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic Binary Search help please  RSS feed

 
Rob Valdivia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, my code is compiling, but is not working the way I want it to, It's supposed to print out index locations for int and words, I just can't seem to figure it out a way to print different string values at different array locations. I have my string array sorted, but I can't figure out how to print what is at index 0, 1, 2, etc or how to see if something isn't in the array.

Please help me out, I've been trying to figure it out for a while now but haven't seen any improvement in how to improve it and make it similar to the example output, also can't see to figure out how is it that plum appears in the example output? (brain burning/sigh)

Example Output:

--------------------Configuration: <Default>--------------------
Integer demo array contains:
1 3 5 7 9 11 13 15 17 19
-6 is not in the array.
-4 is not in the array.
-2 is not in the array.
1 is at index 0
2 is not in the array.
3 is at index 1
4 is not in the array.
5 is at index 2
String demo array contains:
apple orange peach strawberry watermelon
apple is at index 0
plum is not in the array.

Process completed.




 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a second array containg the values you are going to search for.

For the ints this second array will need to contain -6, -4, -2, 1, 2, 3, 4 and 5. You then need a for loop to loop through this array and the contents of the for loop will be lines 33 - 40 of your current code, except that you need to replace the literal 2 with the current value from the array.

For the fruits this second array will need to contain 'apple' and 'plum'. You then need a for loop to loop through this array and the contents of the for loop will be lines 42 - 47 of your current code, except that you need to replace the 'term' variable with the current value from the array.
 
Rob Valdivia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your promptly response and advice Stuart.

So I implemented what you told me however the code is now getting a few errors and I'm not sure how can I be able to use the same result for the second loop? Any words of wisdom, my brain is fried.

Thank you very much in advance!

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you supposed to return -1 for not found; I think Arrays#binarySearch() returns -mid - 1 or similar.
 
Rob Valdivia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell , I think so, I'm supposed to bring the location of some of the variables (Strings, Integers) just as specified below:

Example Output:

Integer demo array contains:
1 3 5 7 9 11 13 15 17 19
-6 is not in the array.
-4 is not in the array.
-2 is not in the array.
1 is at index 0
2 is not in the array.
3 is at index 1
4 is not in the array.
5 is at index 2
String demo array contains:
apple orange peach strawberry watermelon
apple is at index 0
plum is not in the array.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The { on the end of line 38 should be in the end of line 37.
you should be looping through the notfound array, not the digits arrays.
You should be passing an element of the notfound array (notfound[i]) to the search function, not the whole array.

Forget about the fruits for the moment. Comment out lines 19 - 31 and 45 - 53. Let's work on one thing at a time.

And if you still get compilation errors after making thoses chnages, you need to tell us what the errors are.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Valdivia wrote:Thank you Campbell , I think so, I'm supposed to bring the location . . .
You're welcome and -1 would appear to be acceptable.
 
Rob Valdivia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stuart, the errors that I'm getting now say:

BinarySearch.java:36: error: method search in class BSearch<T> cannot be applied to given types;
System.out.println(intBSearch.search(digits));
^
required: Comparable<Integer>
found: Integer[]
reason: actual argument Integer[] cannot be converted to Comparable<Integer> by method invocation conversion
where T is a type-variable:
T extends Comparable<T> declared in class BSearch
C:\Users\SeanRob\Desktop\BinarySearch.java:38: error: method search in class BSearch<T> cannot be applied to given types;
int result = intBSearch.search(notfound);
^
required: Comparable<Integer>
found: Integer[]
reason: actual argument Integer[] cannot be converted to Comparable<Integer> by method invocation conversion
where T is a type-variable:
T extends Comparable<T> declared in class BSearch
2 errors

Process completed.

So does that mean that I have to implement <Integer> instead of Integer[]?



 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you now passing the digits array to your search method ? You were almost right when you were passing the notfound array. You just missed this comment in my last post.
Stuart A. Burkett wrote:You should be passing an element of the notfound array (notfound[i]) to the search function, not the whole array.
 
Rob Valdivia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For everyone following this thread, I just wanted to let you know that I was able to figure it out. I will be posting the completed program tonight.

Big thank you to Stuart A. Burkett, for your input, you helped me figure it out. I love this site, maybe someday I'll be able to help someone like you guys!

Have a great day everyone!

Rob
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!