Thread: Running time question.

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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