Thread: Running time question.

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    122

    Running time question.

    Hi,
    I have an array A of n integers, sorted from min to max, and two numbers a<=b, which are known to be in A. I would like to write a pseudo-code for a procedure whose running time is c1+c2log(n) and which returns the number of elements in A which satisfy a<=A[i]<=b.
    I wrote the following but am not sure it satisfies the requirement for the running time and would appreciate some help:
    *NB <- denotes an arrow*
    Code:
    Range(Input: integer n, sorted array A, integer a, integer b){
    max  <-  n
    i<-BinarySearch(n,A,a)
    num  <-  1
    while (A[i]<=b and i<=max){
        i  <-  i+1
        num  <-  num+1
    }
    output(num)
    }
    where BinarySearch is defined thus:
    Code:
    BinarySearch(Input: integer n, sorted array of integers A, integer x) { 
     min  <-  1, max  <-  n, found  <-  FALSE 
     while ( found = FALSE and min <= max ) { 
     mid  <-  |_(min + max) / 2_| 
     if (A[mid] = x) 
     found  <-  TRUE 
     else if (x < A[mid]) 
     max  <-  mid-1 
     else 
     min  <-  mid+1 
     } 
     if (found = TRUE) 
     output(mid) 
     else 
     output(NOT-FOUND) 
    }
    Last edited by peripatein; 02-23-2014 at 03:46 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Why not just binary search for a, binary search for b, and subtract the index of a from the index of b?

    Array is in sorted order so the difference of indexes must be the number of elements between them. I'd be careful of duplicate values though. Specifically if a and b are not unique in the array.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Well, in case array contains duplicate values, couldn't I simply do this?:
    Code:
    Range(Input: integer n, sorted array A, integer a, integer b){
       max  <-  n
       min  <-  1
       i  <-  BinarySearch(n,A,a)
       j  <-  i
       num  <-  1
       while (A[i]<=b and i<=max){
           i  <-  i+1
           num  <-  num+1
       }
       while (A[j]>=a and j>=min){
           j  <-  j-1
           num  <-  num+1
       }
       output(num)
    }
    Last edited by peripatein; 02-23-2014 at 04:43 AM.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    No. You can't use that approach full stop because you aren't achieving the running time that you wanted. You don't know how many elements are between a and b so your loop could end up running a worst case of O(N) times. This would make your running time O(N) + O(logN).

    You need to use the approach specified in my above post, unless you can think of something else.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by peripatein View Post
    Well, in case array contains duplicate values, couldn't I simply do this?:
    Use the approach outlined by DeadPlanet. To deal with duplicate values, you could specify clearly in your searches which index should be located. For the lower bound, you want the index i of the first element A[i] >= a. When searching for the upper bound, you want the index of the *last* element A[i] <= b. To find the "last element", you could also equivalently find the index of the the first element A[i] > b if it exists and subtract 1 from said index.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I am not quite sure I am following. Let me see whether I correctly understood what you wrote. Were you implying something like this:
    Code:
    Range(Input: integer n, sorted array A, integer a, integer b){
      i  <-  BinarySearch(n,A,A[i]>=a)
      j  <-  BinarySearch(n,A,[A]<=b)
      num  <-  1 + j - i
      output(num)
    }

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It's not clear what the third argument you're supplying is but if you just supply a on the first call and b on the second then that is correct. Again though, extra care needs to be taken if duplicate values exist in the array (see c99tutorial's post).

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    I was actually trying to surmount the duplicate values problem by locating the first occurence of A[i]>=a thus:
    Code:
    BinarySearch(n,A,A[i]>=a)
    That couldn't work, could it? :-)
    Suppose I didn't wish to alter my array, is there a way to locate the first occurence of A[i]>=a without using a loop? However, once I use a loop, wouldn't that entail exceeding the running time restrictions?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Well I mean parameters are just passed by value and it's not like you can pass the index because if you had the index then you wouldn't need to call the function . I know that it's just pseudocode so it's pretty flexible but this very well might change the running time of the programming and isn't as trivial as it first looks so it's worth saying how you would locate the first occurrence of a duplicate element.

    It's likely that if this is an assignment then it says somewhere that there are no duplicates. If not (or if it's not an assignment, or if you want to figure it out anyways!) then I'd probably consider going past the element in my binary search and then backtracking if a same-valued element is not found. Recursion would work well for this. You'd have to have two different methods for if you want to find the index of the first or the last duplicate though. It's not really a particularly elegant solution to the problem and other people might have something better. This would make your implementation consistently perform it's worst case but it would still be more or less logN time.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that the C++ standard library has two generic algorithms available via <algorithm> that could be used here, were this not an exercise to come up with pseudocode and presumably later implement in C: std::lower_bound and std::upper_bound. You could find out how they might be implemented (and since they are function templates, their source code is most likely available in your nearest C++ standard library implementation).
    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

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    122
    Unfortunately, there ARE duplicates in the array (the assignment indicates so). I wrote the following, which seems to work (I have tried it on several arrays), yet am not sure it's infallible:
    Code:
    min  <- 1
    max  <- n
    while (min<=max){
      mid  <-  |_(min+max)/2_|
      if (A[mid]<a)
        min  <-  mid+1
      else
        max  <-  mid-1
    }
    output(min)
    What do you think? Does it seem okay?
    Obviously, in case it works, I could implement pretty much the same for finding the last recurrence of b and that concludes the task.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I think that that works. Nice solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running time
    By wu_weidong in forum Tech Board
    Replies: 1
    Last Post: 09-05-2005, 04:03 PM
  2. Help !!! Time is running out !!!
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-14-2005, 11:57 AM
  3. running time for memchr()
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2003, 11:50 AM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  5. running time
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2002, 09:35 AM

Tags for this Thread