Algorithm for binary search, Data Structure & Algorithms

Q. Write down the algorithm for binary search. Which are the conditions under which sequential search of a list is preferred over the binary search?                                                                   

Ans.

Algorithm for the Binary Search is as follows

1.  if (low> high)

2.  return (-1)

3.  Mid = (low + high)/2

4.  if ( X = = a[mid])

5.  return (mid);

6.  if (X < a[mid])

7.  search for X in a[low] to a[mid-1]

8.  else

9.  search for X in a[mid+1] to a[high]

Sequential Search is preferred over the binary search when the list is disorder and haphazardly constructed. When searching is to be done on unsorted list then linear search is the only option.

Posted Date: 7/12/2012 8:44:24 AM | Location : United States







Related Discussions:- Algorithm for binary search, Assignment Help, Ask Question on Algorithm for binary search, Get Answer, Expert's Help, Algorithm for binary search Discussions

Write discussion on Algorithm for binary search
Your posts are moderated
Related Questions
Determine in brief about the Boolean Carrier set of the Boolean ADT is the set {true, false}. Operations on these values are negation, conjunction, disjunction, conditional,

Explain about Hidden-surface Hidden-line removal refers to wire-frame diagrams without surface rendering and polygonal surfaces with straight edges. Hidden-surface removal ref

Pre-order Traversal The method of doing a pre-order traversal iteratively then has the several steps(suppose that a stack is available to hold pointers to the appropriate nodes

CIRCULARLY LINKED LISTS IMPLEMENTATION A linked list wherein the last element points to the first element is called as CIRCULAR linked list. The chains do not specified first o

There are three kinds of tree traversals, namely, Postorder , Preorder and Inorder. Preorder traversal: Each of nodes is visited before its children are visited; first the roo

What are the Dynamic arrays Dynamic arrays are convenient for programmers since they can never be too small-whenever more space is needed in a dynamic array, it can simply be e

State about the pre- and post conditions Programmers can easily document other pre- and post conditions and class invariants, though, and insert code to check most value preco

For preorder traversal, in the worst case, the stack will rise to size n/2, where n refer to number of nodes in the tree. Another method of traversing binary tree non-recursively t

How to measure the algorithm's efficiency? It is logical to examine the algorithm's efficiency as a function of some parameter n showing the algorithm's input size. Instance

Question 1 Describe the following- Well known Sorting Algorithms Divide and Conquer Techniques Question 2 Describe in your own words the different asymptotic func