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

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

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Question 1 Discuss the advantages of implementation checks preconditions Question 2 Write a ‘C' program to search for an item using binary search Question 3 Show that To

Write an algorithm for binary search. Algorithm for Binary Search 1.  if (low> high) 2.  return (-1) 3.  Mid = (low + high)/2 4.  if ( X = = a[mid]) 5.  return (mid); 6.

In this project you will write a program to produce a discrete time simulation of a queue as shown in Fig. 1. Time is slotted on the input and the output. Each input packet follows

The data structure needed to evaluate a postfix expression is  Stack

nested for loop for (i = 0; i for (j = 0; j sequence of statements } } Here, we observe that, the outer loop executes n times. Every time the outer loop execute

Q. Write  down a   programme  in  C  to  create  a  single  linked  list also  write the functions to do the following operations (i)  To insert a new node at the end (ii

adjacency multilist