answersLogoWhite

0

Write an algorithm with analysis steps for binary search?

Updated: 8/18/2019
User Avatar

Wiki User

6y ago

Best Answer

Given a sorted data sequence, if the data sequence is empty, return NULL to indicate the value does not exist, otherwise compare the middle element's value with the value being searched. If there's a match, return a reference to that element. If there is no match and the value being searched is smaller, repeat the search upon the lower half of the sequence, otherwise repeat the search on the upper half of the sequence.

The data sequence must be in sorted order. With each unsuccessful iteration of the algorithm, the order allows us to eliminate half of the remaining elements. Typically, the data sequence will be implemented as an array (or vector) as this allows constant-time random-access to the middle element of the sequence. A binary tree can also be used but, for efficiency, the tree must be balanced. That is, for any subtree within the tree, there must be the same number of nodes under both the left and right branches or no more than 1 node difference. With a balanced binary tree, every node is the middle element of its subtree. Thus, for every unsuccessful iteration, we simply traverse left or right, starting at the root.

The best case time complexity for binary search of n elements is O(1), where the element is found in the first iteration (and is therefore the middle element). The worst case is O(log n) - the binary logarithm of n.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write an algorithm with analysis steps for binary search?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is algorithm to write algorithm to the program to access a pointer variable in structure?

Here is the algorithm of the algorithm to write an algorithm to access a pointer in a variable. Algorithmically.name_of_the_structure dot name_of_the _field,eg:mystruct.pointerfield


Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation


A Write the algorithm to concatenate two given strings?

a write the algorithm to concatenate two given string


Write an algorithm and draw a corresponding flowchart to search a number in the given list of numbers and also display its position?

please give me an algorithm and a corresponding flow chart that displays list of numbers from 1 to 20.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Write an algorithm and draw flowchart to calculate the perimeter of a square?

write an algorithm and draw a flow chart to find perimeter of a square


What is algorithm and what are its characteristics?

An "algorithm" is simply a method to solve a certain problem. For example, when you use the standard method you learned in school to write down two numbers, one beneath the other, then add them, you are using an algorithm - a method that is known to give correct results in this particular case.


Design step by steps algorithm on how to write the letter A and display the result?

Design step by steps algorithm on how to write the letter A and display the result


Write an algorithm to check whether a number is a prime number or not?

You can write out this algorithm. This will then be programmed into the device to make determining prime numbers easier.


How do you write 23 binary?

Decimal 23 is 10111 in binary


How do you write 18 as binary?

Decimal 18 is 10010 in binary


How do you write 26 in binary?

Decimal 26 is 11010 in binary