-1

This is the pseudo code that i want to calculate time complexity ,i think it is a binary search algorithm but i fail when calculating the complexity because it is reducing logarithamic.

   USE variables half-array,found,middle element
   SET half-array=initial array;
   SET found=True;

 Boolean SearchArray(half-array)

   find middle element in half-array;
   Compare search key with middle element;
   IF middle element==search key THEN
           SET found=True;
   ELSE
        IF search key< middle element THEN
          SET half-array=lower half of initial array;
        ELSE
          SET half-array=upper half of initial array;


 SearchArray(half-array)
kalsara Magamage
  • 263
  • 2
  • 16
  • Are you only running the method once, or are you running it recursively? – obizues Apr 26 '17 at 13:49
  • run it recursively – kalsara Magamage Apr 26 '17 at 13:50
  • its binary search, log(n) – nafas Apr 26 '17 at 13:50
  • It looks like you are running this method recursively, and with each iteration you are reducing the number of elements being searched by half. This is going to be a logarithmic reduction, i.e. O(log n). – Rion Williams Apr 26 '17 at 13:51
  • What is the answer? Is it O(log n) – kalsara Magamage Apr 26 '17 at 13:53
  • @kalsaraMagamage as Rion and nafas stated, yes it is – XtremeBaumer Apr 26 '17 at 13:55
  • http://stackoverflow.com/questions/10369563/difference-between-on-and-ologn-which-is-better-and-what-exactly-is-olo Think of O(n) as a single enumeration on a List of size n. O(log n) would be when the algorithm is much more efficient and doesn't enumerate the entire List, but uses logic to find the answer quicker as in a binary search – Novaterata Apr 26 '17 at 13:58
  • 2
    Possible duplicate of [how to calculate binary search complexity](http://stackoverflow.com/questions/8185079/how-to-calculate-binary-search-complexity) – xandermonkey Apr 26 '17 at 13:58
  • It is `O(42)`. Why? Because `42` is the answer of all questions available in the universe. – Harmlezz Apr 26 '17 at 13:59
  • Rote learning of answers one question at a time will not teach you. You need to purchase and study https://en.m.wikipedia.org/wiki/Introduction_to_Algorithms – Lew Bloch Apr 26 '17 at 16:35

3 Answers3

3

It looks like you are running this method recursively, and with each iteration you are reducing the number of elements being searched by half. This is going to be a logarithmic reduction, i.e. O(log n).

Since you are reducing your elements by half each time, you need to determine how many executions will be needed to reduce it to a single element, which this previous answer provides a proof or if you are a more visual person, you can use the following diagram from this response:

enter image description here

Community
  • 1
  • 1
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

Yes,It is indeed a binary search algorithm.The reason why it is called a 'binary' search is because,if you would have noticed,after each iteration,your problem space is reduced by roughly half (I say roughly because of the floor function). So now,to find the complexity,we have to devise a recurrence relation,which we can use to determine the worst-case time complexity of binary-search.

Let T(n) denote the number of comparisons binary search does for n elements.In the worst case,no element is found.Also,to make our analysis easier,assume that n is a power of 2.

BINARY SEARCH:

  1. When there is a single element,there is only one check,hence T(1) = 1.

  2. It calculates the middle entry then compares it with our key.If it is equal to the key,it returns the index,otherwise it halves the range by updating upper and lower bounds such that n/2 elements are in the range.

  3. We then check only one of the two halves,and this is done recursively until a single element is left.

Hence,we get the recurrence relation:

T(n) = T(n/2) + 1

Using the Master Theorem,we get the time complexity to be T(n) ∈ Θ(log n)

Also refer : Master Theorem

0

You are correct in saying that this algorithm is Binary Search (compare your pseudo code to the pseudo code on this Wikipedia page: Binary Search)

That being the case, this algorithm has a worst case time complexity of O(log n), where n is the number of elements in the given array. This is due to the fact that in every recursive call where you don't find the target element, you divide the array in half.

This reduction process is logarithmic because at the end of this algorithm, you will have reduced the list to a single element by dividing the number of elements that still need to be checked by 2 - the number of times you do that is roughly equivalent (see below) to the number of times you would have to multiply 2 by itself to obtain a number equal to the size of the given array.

*I say roughly above because the number of recursive calls made is always going to be an integral value, whereas the power you would have to raise 2 to will not be an integer if the size of the given list is not a power of two.