Binary search, Computer Engineering

Assignment Help:

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.


Related Discussions:- Binary search

Sequential execution of instructions in risc, Q. Sequential Execution of In...

Q. Sequential Execution of Instructions in RISC? Let's describe pipelining in RISC with an illustration program execution sample. Take the given program (R denotes register).

Show organisation of clos network, Q. Show Organisation of Clos network? ...

Q. Show Organisation of Clos network? Clos network: This network was designed by Clos (1953).  It's a non-blocking network and offers full connectivity similar to crossbar netw

Explain about hexadecimal numbers system, Q. Explain about Hexadecimal Numb...

Q. Explain about Hexadecimal Numbers system? Hexadecimal system has 16 digits that are represented as 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. A number (F2) H is equivalent to

Explain register addressing mode, Q. Explain Register Addressing Mode? ...

Q. Explain Register Addressing Mode? Operand can be a 16-bit register: Addressing Mode Description Example AX, BX, CX, DX, SI,

Print a prompt, When your shell is waiting for input from the user, it shou...

When your shell is waiting for input from the user, it should first print a prompt. The prompt should consist of the current working directory followed by the _>_ character. Here i

Simulate a worm propagation considering traffic delay, At time t when an in...

At time t when an infected machine scans and finds a vulnerable machine, the vulnerable one will be compromised and start to scan and infect others at time t+X, where X is a r.v. f

Computer Graphics, Distinguish between uniform scaling differential scaling...

Distinguish between uniform scaling differential scaling

Describe the size and speed of scanners, Q. Describe the Size and Speed of ...

Q. Describe the Size and Speed of Scanners? Before actual scanning, a quick, low resolution scan known as pre-scan is made to preview the image and select scanning area. After

Explain about microprocessors, Q. Explain about Microprocessors? Microp...

Q. Explain about Microprocessors? Microprocessors have instruction sets ranging from 20 to several hundred instructions, known as microprograms, which are stored in ROM to init

Computer graphics, what isAdvantages of scan line algorithm, Computer Graph...

what isAdvantages of scan line algorithm, Computer Graphics?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd