So this algorithm can be used with the sorted array only? What happens if the array is [3, 31, 2, 23, 1, 40]
Hey, hi! Yes, you can only use this algorithm if the array is sorted.
I'll explain using your array as an example:
Say you have [3, 31, 2, 23, 1, 40], and you're trying to find the number 31. Say, too, that the mid-point is 2 (or even 23, in this case).
In its very first step, the algorithm would ignore everything that's to left of 2, since 31 is higher than 2.
That means that we would end up with:
[2, 23, 1, 40].
Now, it's impossible to find 31.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
So this algorithm can be used with the sorted array only? What happens if the array is [3, 31, 2, 23, 1, 40]
Hey, hi! Yes, you can only use this algorithm if the array is sorted.
I'll explain using your array as an example:
Say you have [3, 31, 2, 23, 1, 40], and you're trying to find the number 31. Say, too, that the mid-point is 2 (or even 23, in this case).
In its very first step, the algorithm would ignore everything that's to left of 2, since 31 is higher than 2.
That means that we would end up with:
[2, 23, 1, 40].
Now, it's impossible to find 31.