×

# [closed] Binary search function - STL

 2 1 I have created a simple array (not a vector, template, class...nothing of that sort) and I want to use a nice and neat binary search function from the stl to find a particular value in the array and return its index. Oh yes, the first thing I did is google "binary search function stl." And voila! So many results. But everything involves Random iterator, template, class, forward iterator etc and since I haven't learnt the OOPS part of C++ yet (Not that I am shying away from it, but I am just trying to put my Non-OOP knowledge to work), I am unable to comprehend those complicated terms. Just like how for sorting an array, all I have to have is sort(Arr,Arr+N); with an algorithm header on top, could someone suggest a similar simple away of using the binary search function to find an element and return its index in this simple array of mine? asked 29 Nov '14, 18:50 2★sandy999 391●1●16●38 accept rate: 10% 4★kunal361 6.0k●13●32●72 Thanks for asking this question, was just searching for it on internet... (29 Nov '14, 20:56)

### The question has been closed for the following reason "The question is answered, right answer was accepted" by kunal361 01 Dec '14, 18:03

 1 Hope this will help...:) You can read the description of the return value here!! answered 29 Nov '14, 19:51 4★kunal361 6.0k●13●32●72 accept rate: 21% Hey @kunal361 Can you please tell me why isn't this piece working int main() { int arr[]={9,77,3,4,5,6,7,8,9}; sort(arr.begin(),arr.end()); cout<
 1 When r-l=1 arr[l] is the largest element smaller than the key and arr[r] is the smallest element larger than the key....hope this helps...:) answered 30 Nov '14, 18:20 4★kunal361 6.0k●13●32●72 accept rate: 21% Of course it helped! :) Thank you soo sooo much! I asked this question because I wanted to implement this idea of binary search in a ZCO 2012 problem WORMHOLES (I've never used binary search in a question before, always pulled on with linear search) although I haven't completely got the problem right yet. (01 Dec '14, 16:32) sandy9992★ Can u provide the problem link?? Also if this has answered your question...can I close this thread?? (01 Dec '14, 18:01) kunal3614★ 1 I have posted it as a separate query as part of discussion on the same problem. It's here http://discuss.codechef.com/questions/56739/zco-2012-problem-wormholes And yeah please close this thread. (01 Dec '14, 18:03) sandy9992★
 0 Thank you so much @kunal361 This is a kind of follow up question. Suppose the element ‘a’ is not present in my simple array. But instead, I am looking for an element ‘b’ which is nearest to ‘a’ and just less than ‘a’. Also, I am looking for an element ‘c’ which is nearest to ‘a’ and just more than ‘a’. How do I tweak binary search to implement this? I don't need an STL function, but a general procedure as I am unable to understand the behaviour of the mid value in the binary search implementation to make sure the above thing works. answered 30 Nov '14, 17:38 2★sandy999 391●1●16●38 accept rate: 10%

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,056
×279

question asked: 29 Nov '14, 18:50

question was seen: 4,024 times

last updated: 01 Dec '14, 18:03