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
The controller responds to user interactions, with the application, by selecting the act method to implement and also selecting the view to render.

What are the components of I-way Infrastructure? There are three components of the I-way infrastructure: • Consumer access equipment • Local on-Ramps • Global informat

Q. Show Arithmetic Subtraction? The subtraction can be done easily using 2's complement by taking 2's complement of value which is to be subtracted (inclusive of sign bit) and

What is ROM? Read only memory [ROM] is used for storing programs that are permanently resident in the computer and for tables of constants that do not change in value as the pr

Question: a) The following is a description of the navigation options in a mobile web site which provides public bus time slots for a city. It is an interesting service for m

Define Grammar of a language. A formal language grammar is a set of formation rules which describe that strings formed from the alphabet of a formal language are syntactically

Q. Explain Data Parallel Programming? In data parallel programming model major focus is on performing concurrent operations on a data set. The data set is characteristically or

How Web-Based Word Processing Works? Web-based word processors are hosted in the cloud, not on the hard drive-as are the documents formed with these applications. And these web

Q. Which things should microprocessor validate? A microprocessor should validate: Powerful addressing modes More addressing capability More throughput Po

Proving ( or disproving) mathematical theorems are an intensive intellectual task. This is because it requires deductions from hypotheses and also involves judgment. This judgm