Thread: C++ problem with binary search algorithm

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34

    C++ problem with binary search algorithm

    Hello,
    I have to made a programme which will search for given number and it must work in O(log(n)). The problem is that this programme beside finding this number have to find how many times this given number is used in this sequence.

    Sequence is from lowest to highest only one possibility to use binary search algorithm

    For example when we have squence
    -1 -2 3 3 3 3 4 5 6 7 7 8 9 9 9 9 10
    The numbers we need to search are
    1 , 3 , 7 , 9 , 11 , 12
    The answer is
    0 , 4 , 2 , 4 , 0 , 0
    So we need to find the sum of used number in sequence.

    I have written algorithm
    Code:
    int start = 0; 
    int end = sequencelenght - 1; 
    int mid = 0;  
    /// Binary serach
    while (start<=end) {
       int mid=(start+end)/2; 
       if (sequence[mid]==givennumber) {
       intright=mid+1; 
       int left=mid-1;
       sum++; 
    /// Here is problem with searching duplicates its O(n) not O(log(n))
       while(left >= pocz && array[left]==givennumber){
                                sum++;
                                left--;
                            }
      while(right <= end && array[right]==givennumber){
                                sum++;
                                right++;
                            }
    ////// End of problem ///////
     cout << sum;
                            break;
                        } 
      if (array[mid]<givennumber) {
         start=mid+1;
      } else {
         end=mid-1;
                        }
                    }
        if(sum==0)
                    {
                        cout << "0";
    }
    }
    As u see i search for given numer with binary with O(log(n)) but when i have to sum the duplicates the only good way i see is using loop to right and left but this have got log(n) specification
    (becouse when sequence would be for example
    7 7 7 7 7 7 7 7 7 and given number to search will be 7 this will be O(n) loop).
    Can somebody tell me how would look the most optimal algorithm look for this exercise? I mean O(log(n)) the fastest algorithm becouse i have no idea how to do this better.

    Sorry for my English and thanks for help.

  2. #2
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Only one idea what comes to my is to doing binary search to find duplicats i mean when we find given number in search we know about 2 positions left and right we can delete this and again do search binary for given number and repeat this and repeat but i dont know that if this will be O(log(n)) or O(2log(n)) and i still dont know that would be most optimal algorithm or not. (repeating binary search)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would use std::lower_bound and std::upper_bound. Of course, since you have to implement the binary search yourself, what you can do is to investigate how these two standard generic algorithms work and how you might implement them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Yep i cant use std:lower_bound/upper_bound. But i have problem with searching what complexity programme has, i mean finding the number in sorted sequence with binary search is O(log(n)) with using normal loop from start to end it will be O(n)
    but when i had to find those duplicats and i will do binary search and when i find MID i can find the index + 1 from mid and index - 1 from mid, i can delete this if there were duplicats the sum will be 3 (mid-1 + mid+1 + mid ) and then do again binary search and if there still be this same number i can do this again look for left side and right side if they are not it will stop if they will be again duplicats it will repeat this. I dont know what complexity this way would have. It would be O(2log(n)) or something else? I had to make the most optimal function to find those duplicats :P the only one tip was that programme with one function start will be for max points, programme with 2 functions starting will be for half points and it has to be O(log(n)), finding mid and then going with loop to right and left to search duplicats is good but where there will be pesimistic sequence it will be O(n) algorithm so this programme is bad.
    Last edited by aatrox; 03-27-2014 at 05:36 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by aatrox View Post
    I dont know that if this will be O(log(n)) or O(2log(n))
    O(log(n)) is equal to O(2log(n)). The number of operations is proportional to log(n).

    It might be easier to think recursively.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Yep but i cannot make id recursively i must do it with using iteration (I dont know how to write it correctly)

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I suggested thinking recursively - get your common logic right. Coding recursively is not required.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary search with string algorithm
    By kabuki in forum C Programming
    Replies: 4
    Last Post: 10-03-2013, 01:11 PM
  2. problem in binary search
    By LINUX in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 08:50 AM
  3. binary search algorithm problem...
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 09-29-2005, 03:28 PM
  4. Binary Search Tree Insertion Algorithm Help
    By bcianfrocca in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 07:35 PM
  5. Ornery binary search algorithm
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 03:32 PM