Binary search, Computer Engineering

Binary Search

Now that the basic framework is working, it is time to begin implementing a few alternative search functions. Each of these search algorithms have strengths and weaknesses, depending on the distribution of the input and the search keys used. The classic solution to this problem is binary search. Binary search is a divide-and-conquer algorithm. See Levitin [2007] pp 162 for a detailed description of this algorithm.

ALGORITHM BinarySearch (A[0 . . . n - 1], k)
// Non-recursive binary search in an ordered list.
// INPUT : An array A[0 . . . n - 1] of ordered elements, and a search key k.
// OUTPUT : an index to the position of k in A if k is found or -1 otherwise.
1: l ← 0; r ← n - 1
2: while l ≤ r do
3: m ← ⌊(l + r)/2⌋
4: if A[m] = k then
5: return m
6: else if k < A[m] then
7: r ← m- 1
8: else

9: l ← m+ 1

10: return -1

Algorithm BinarySearch shows the pseudocode for this solution. Implement the algorithm.

Posted Date: 2/23/2013 6:44:24 AM | Location : United States







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

Write discussion on Binary search
Your posts are moderated
Related Questions
Explain briefly the function of different layers which are covered under end to end layer connectivity. Different layer are as follows: 1. Transport Layer:  This is res

What are the some support activities in primary functions of Supply Chain Management? To support primary functions of Supply Chain Management there will be a company’s infrastr

Artificial Life - artificial intelligence: Give birth to new exits forms.  A swot of Artificial Life will certainly direct on what it means for a complex system to be 'aliv

A doubly linked list is like a linked list except that each node has a pointer both to the next node in the list and to the previous node in the list. There are also pointers to th

Q. Explain Extended Binary Coded Decimal Interchange Code? Extended Binary Coded Decimal Interchange Code (EBCDIC) is a character-encoding format employed by IBM mainframes. It

Explain about subsystem and object of object oriented modeling A subsystem is a grouping of elements of that constitutes a specification of behaviour offered by other contained

Q. Show the developments that happened in third generation? The main developments that happened in third generation can be summarized as below: Application of IC circuit

Explain briefly the generic framework for e-commerce.  Generic framework of e-commerce contains the Applications of EC   (like as banking, shopping in online stores and malls,

What are the main data structures used in the following areas: RDBMS, Network data model and Hierarchical data model? RDBMS = Array (i.e. Array of structures) Network data m

Explain the relationship amongst Translated address and Linked address. Translated address: Address assigned through the translator Linked address: Address assigned