0

I have an array like MyArr {1,3,5,7,9,2,4,6,8,10}. I need to iterate and print "Not Found" till I reach 2. After that, I need to print "Found" for the remaining.

My approach is to use Arrays.BinarySearch(MyArr,2) which returns the index of 2. I have no idea how to achieve from here.

9
  • 5
    can you show what you have tried so far? Commented May 7, 2016 at 6:37
  • 2
    The method is called binarySearch, not BinarySearch, and javadoc says: The array must be sorted. Your array is not sorted, so you cannot use binarySearch. Time for you to iterate the array yourself, which is actually is big part of your assignment. Commented May 7, 2016 at 6:42
  • Who says he needs to use binary search? Just iterate over the array once be done with it. Commented May 7, 2016 at 6:52
  • @TimBiegeleisen I don't think anyone here did. That was OP's own idea. Hence I suggested the same thing (iterate, that is). Commented May 7, 2016 at 6:53
  • 1
    It's so easy. You should read documentation sometimes. Look at docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html. Commented May 7, 2016 at 7:19

2 Answers 2

4

Binary search can't be used because it only works on sorted arrays.

You need to iterate over the array. For each element, you must check if it's your target value and have your code remember the result, and print the output appropriate for the value of the result.

Sign up to request clarification or add additional context in comments.

3 Comments

binarySearch doesn't find the value, it finds the index, which would have been valuable, had it worked (sorted array), so your second bullet seems wrong.
Yay! I've killed 5 answers so far (by down-voting them). Finally an answer I can up-vote (guess because it mostly says the same as my comment?). It tells what is wrong, how to do it better, without giving away the solution. Yay!
@andreas I don't know what I was thinking :/. Bullet deleted
0

binarySearch only works for an input array that is sorted (in ascending order), and your input array isn't sorted.

Solution #1:

Sort the array first. Then binarySearch will find you the correct offset for the element in the sorted array.

Hint: look at the other methods in Arrays.

Note that this is not the correct solution. The actual problem statement says that you need to step through the array, 1) printing "not found" for non-matching elements and 2) printing "found" when you find the first match. Solution #1 only addresses the 2nd requirement, not the first one.

In fact, binary search cannot satisfy the first requirement.

Aside: sorting an array so that you can do a binary search ... just once ... is inefficient. You spend more time sorting than is saved in searching. In complexity terms, sorting will be O(NlogN) and searching O(logN) giving an overall complexity of O(NlogN). By contrast a simple linear search is O(N). Hence you will only "break even" if you do O(logN) binary searches for each sort.

Solution #2:

Forget about binary search, and write a loop that steps through all of the elements of the array.

Hint: a for loop would be best, but which kind of for loop?

5 Comments

Sorting the array first would produce incorrect result. Solution should print Not Found 5 times, followed by Found 5 times. If you sort, the number 2 will be in a different position and output would be Not Found 1 time, and Found 9 times.
So 2/3 of your answer is a solution that cannot be used (sorting)?
Yes. And it is useful for the OP to understand the reasons (plural!!!) why he can't / shouldn't.
Sorry, maybe I'm just ornery at this late hour (3am), but your description of why not is off. You say "print found when you find the array", but it's supposed to be "print Found when you find the value, then print Found for the remaining". The entire point of why you cannot sort, is that it changes the position of the value you're looking for, which even makes your initial statement wrong: "binarySearch will find you the correct offset". It won't, because it wouldn't be the correct offset, and so it doesn't even address the 2nd requirement.
Disagree, but not enough to down-vote you, so I'll leave it neutral and go to bed. To totally kill a Star Trek quote: May you be friendly and helpful. (more than me, for sure)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.