Question

In: Computer Science

What is the Big-Oh notation of the following code snippet: BinarySearch(numbers, N, key) { mid =...

What is the Big-Oh notation of the following code snippet:

BinarySearch(numbers, N, key) {
    mid = 0;
   low = 0;
   high = 0;
   
   high = N - 1;
   
   while (high >= low) {
      mid = (high + low) / 2
      if (numbers[mid] < key) {
         low = mid + 1
      }
      else if (numbers[mid] > key) {
         high = mid - 1
      }
      else {
         return mid
      }
   }
   
   return -1   // not found
}

Solutions

Expert Solution

Big-oh notation means the worst case complexity. The code snippet we are inspecting is a binary search algorithm.

The worst case complexity of binary search is log2N where N is the number of elements. It occurs when the search key is located at first or last index of the list.

Let's take an example:

List = 1, 2, 3, 4, 5, 6, 7, 8, 9

N = 9

if key = 1:

then, first pass:

mid = (8+0)/2 = 4, list[mid] is 5, and 1 is less than 5 so high becomes 4-1=3, in second pass:

mid = (3+0)/2 = 1,list[mid] is 2, and 1 is less than 2 so high becomes 1-1 = 0, in third pass:

mid = (0+0)/2 = 0, list[mid] is 1 and key is also 1.

So search is completed after 3 passes. 3 comparisons are required.

Now let's calculate log29 = 3.16 which is approximately 3.

So our example satisfies Big-oh.

Note : mid value is calculated as integer that's why only the value before decimal point.

You can further test your code with other lists and extreme values. Add a count variable in your code and see the count results for different combinations. You will always find that O(log2N ) for this code.

If you have any query then ask me in comment section. Please give a thumbs up if you find the answer helpful.


Related Solutions

Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation...
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation in terms of the variable n. Show your work a) O( ) int m1(int n, int m) { if (n < 10) return n; else if (n < 100) return happy (n - 2, m); else return happy (n/2, m); } ----------------------------- b) O ( ) void m2 (int n) { j = 0; while (j < n) { for (int i = 0;...
What is the output of the following code snippet? (If there is some kind of syntax...
What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int laps = 8;        if (laps++ > --laps)               laps += 2;        else if (--laps > (laps - 1))               laps += 4;        else if (++laps)               laps -= 3;        cout << laps << endl; 2. What is the output of the following code snippet?     int j = 47;     int i = 8;     j =...
1. Give the O-runtime depending on n for the code snippet below (n > 0). for(...
1. Give the O-runtime depending on n for the code snippet below (n > 0). for( j = n; j <= 0; j = j - 1)                                     for( k = 1; k <= n; k = k + 1)                                                 print(" "); 2. Give the O-runtime depending on n for the code snippet below (n > 0).            for( j = 1; j <= n; j = j + 1)                                     for( k = 1; k <= n; k =...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
Identify all private and shared variables in the following code snippet. What is the output of...
Identify all private and shared variables in the following code snippet. What is the output of the program (e.g. the value of res)? // ========= int A[100]; int count; void work(int index[]) { float B[10]; …… } void main(){ int res = 10; #pragma omp parallel   { int index[10], i=0, IS=0;                         work(index);                         #pragma omp for                         for(i=0;i<100;i++)                         { IS = IS + i;                         } #pragma omp critical res += IS; }...
The following code snippet is free of errors (issues that will prevent the code from compiling)...
The following code snippet is free of errors (issues that will prevent the code from compiling) but it does contain a major bug that will cause the program to hang (not finish running). Identify the problem and create a line of code to fix it. #include <stdio.h> char get_input(); int main() { printf("The user's inputted letter is %c\n", get_input()); return 0; } char get_input() { printf("Please enter a letter: \n"); char input = getchar(); while (!(input >= 'a' && input...
Justify the following statements using the "big-Oh" definition: a) (n+25)2 is O(n2) , and n2 is...
Justify the following statements using the "big-Oh" definition: a) (n+25)2 is O(n2) , and n2 is O((n+25)2) b) n3 is NOT O(n2); c) Given f1(n) is (n+25)2 , and f2(n) is n3 what is the big-Oh for f1(n) x f2(n)?
What is the Pople Notation of the ethyl group in ethanol CH3-CH2-OH
What is the Pople Notation of the ethyl group in ethanol CH3-CH2-OH
• Based on the following code snippet and plan attributes, provide the output for Plan A,...
• Based on the following code snippet and plan attributes, provide the output for Plan A, Plan B and Plan C. Code Snippet • {{IF:[Copay]=Not Covered && [Coinsurance]=Not Covered, show ‘Not covered’}} • {{IF:[Copay]!=$0.00 && [Copay]!=Blank, show value from {{Copay}} copayment}} • {{IIF:[Copay]!=$0.00 && [CopayFrequency]!=Blank, show {{Copay}} copayment/{{CopayFrequency}}}} • {{IF:[CopayMax]=$0.00 OR [Copay]=$0.00, show No Charge}} • {{IF:[[CopayMax]!=$0.00 && [CopayMax] contains ‘$’ && [Coinsurance] contains ‘%’ && [CopayMaxFrequency]=Blank, show {{CopayMax}} copayment then {{Coinsurance}} coinsurance]}} Plan Copay CopayFrequency CopayMax CopayMaxFrequency Coinsurance Plan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT