C Coding question on running time program

/*assume contents are sorted*/
Public static intbinarySearch(int x,int a[]){
int low=0; 0(1)
int high=a.length-1; 0(1)
int mid; 0(1)

while(low<=high){
mid=(low+high)/2
if(a[mid]low=mid+1;
else if(a[mid]>x)
high=mid-1;
else
return mid;
}
return -1;
}


---------------------

n=0 0(1)
sum=0; 0(1)
cin>>x;
while(x!=99)
{
n++; 0(1)
sum+=x; 0( n/x)
cin>>x;
}
mean - sum/n;

what is the running time?
Please help me understand it. The websites are not that helpful to me. Please. Please if you're not that busy can you please answer it with explanations?:) :) :) THANK YOU!!

Questions by Joanne Tan

Showing Answers 1 - 3 of 3 Answers

Sereche

  • Jul 9th, 2012
 

This seems to be a question about time complexity / big-O notation. Time complexity basically tells you how the program running time scales, as a function of one of the variables of input (in most cases, the size of the input). Your first function (the binary search) runs in O(log n) time, the second runs in O(n) time. What this means is best illustrated with an example:

Lets say that running your binary search function on an array of 1,000 elements takes, on average, 1 second. You would then expect the function to take about 2 seconds to run on an array of 1,000,000 elements, and about 3 seconds on an array of 1 billion elements. Similarly, with an O(n)-time algorithm, you would expect the running time to roughly double when you double the size of the input set.

Note that these estimates are just that, estimates. At low values of n, the information is almost meaningless, as overhead costs become a larger factor. In addition, not all O(n) algorithms will run in the same amount of time, though, for large enough n, those times should differ by a constant factor (i.e. one O(n) algorithm takes roughly twice as long as a different O(n) algorithm).

For more information on time complexity, I would recommend reading http://en.wikipedia.org/wiki/Time_complexity.


Also, note that the code youve provided is not valid C (or, for that matter, any language that I can recognize). The public/static qualifiers are meaningless in C (C#, maybe?). Passing an array as a parameter is not done correctly (and is typically not done at all in C or C++; again, did you mean C#?). Since youve tagged this question as C++, and put C in the title, Ive included code which should be valid in both C/C++. (Note: I havent actually tested it, so there is always the possibility of some small error)

Code
  1. int intBinarySearch(int x, int* a, int length) {

  2.    int low = 0;

  3.    int high = length - 1;

  4.    int mid;

  5.  

  6.    while (low <= high) {

  7.       mid = (low + high) / 2

  8.       if(a[mid] < x)

  9.          low = mid + 1;

  10.       else if (a[mid] > x)

  11.          high = mid - 1;

  12.       else

  13.          return mid;

  14.    }

  15.    return -1;

  16. }

  17.  

  18.  

  19. int findMeanOfInput() {

  20.    int n = 0;

  21.    int sum = 0;

  22.    int x;

  23.    

  24.    //Note that the following three lines can be replaced with

  25.    // cin >> x;

  26.    //if you are writing in C++

  27.    char buf[1024];

  28.    gets(buf);          

  29.    x = atoi(buf);

  30.  

  31.    while(x != 99)

  32.    {

  33.       ++n;

  34.       sum += x;

  35.  

  36.       //Again, these three lines can be replace with cin in C++

  37.       char buf[1024];

  38.       gets(buf);          

  39.       x = atoi(buf);

  40.    }

  41.    return sum / n;

  42. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions