This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayCopy in Binary Search  RSS feed

 
Isan Nasi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help with my code here please. I wanted it display the arrays every time it is splitted while containing the key value until it arrives with the simplest array with key value in it then it display "Found!". My problem is, it worked for key = 2 only and not others keys. Please help me for that.

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

If that is the output you wanted, what is the exact output you are seeing? What happens if you seek a number in the upper half of the array, maybe 11?
 
Knute Snortum
Sheriff
Posts: 3834
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has to do with the length of the temp array.  Think about what its size should be, especially if the "upper" half (greater than mid) is being copied.
 
Vinod Khatrii
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to copy the array contents for the given requirement. This would unnecessarily need more memory and processing.

We can use the index of the search to print the same. Please find the code below:

[code removed by moderator, see reply below]
 
Knute Snortum
Sheriff
Posts: 3834
91
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank your, Vinod Khatrii, for your contribution.  The reason your code was removed is that in the Beginning Java forum, we don't give out complete solutions until the OP has posted their own.  This is because we want the forum to be a place where the posters can learn from our guidance.  Posting snippets of code it okay.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!