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
Abstract Data Types :- A useful tool for specifying the logical properties of a data type is the abstract data type or ADT. The term "abstract data type" refers to the basic mathem

Tree is dynamic data structures. Trees can expand & contract as the program executes and are implemented via pointers. A tree deallocates memory whereas an element is deleted.

Program: Creation of Doubly Linked List OUTPUT Input the values of the element -1111 to come out : 1 Input the values of the element -1111 to come out : 2 Inpu

The above 3 cases are also considered conversely while the parent of Z is to the right of its own parent. All the different kind of cases can be illustrated through an instance. Le

a) Find the shortest paths from r to all other nodes in the digraph G=(V,E) shown below using the Bellman-Ford algorithm (as taught in class). Please show your work, and draw the f

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

In assignment, you have already started the process of designing a database for the Beauty Salon mini-case (enclosed again below), mainly in the phase of conceptual database design

Question 1 What do you mean by Amortization? Question 2 Explain the following Big Oh notation (O) Omega notation (Ω) Theta notation (Θ)   Question 3 Di

Explain Dijkstra's algorithm Dijkstra's algorithm: This problem is concerned with finding the least cost path from an originating node in a weighted graph to a destination node

Q. Define a method for keeping two stacks within a single linear array S in such a way that neither stack overflows until entire array is used and a whole stack is never shifted to