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
perform the following length operation LENGTH("welcome to ICA")=

Define Spanning Tree A Spanning Tree of a connected graph is its linked acyclic sub graph (i.e., a tree) that having all the vertices of the graph.

Conversion of Forest into Tree A binary tree may be used to show an entire forest, since the next pointer in the root of a tree can be used to point to the next tree of the for

Sorting Algorithm A sorting algorithm is an algorithm which puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Eff

Question 1 Discuss the following theorems with respect to Splay Trees- Balance Theorem Dynamic Finger Theorem   Question 2 Write a C program for implementation

HLS Colour Model  This model has the double-cone representation shown in Figure 3.40. The three colour parameters in this model are called hue (H), lightness (L), and Saturati

need c++ algorithmic software program to derive one numerical outcome from 10 levels of variables with 135 combinations cross computed

Define about the class invariant A class invariant may not be true during execution of a public operation though it should be true between executions of public operations. For

1. What is an expert system and where are they needed? 2. What are the major issues involved in building an expert system?

WRITE AN ALGORITHM TO READ TWO NUMBERS AND PRINT THE LOWER VALUE