Example of binary search, Data Structure & Algorithms

Assignment Help:

Let us assume a file of 5 records that means n = 5

And k is a sorted array of keys of those 5 records.

1185_Example.png

Let key = 55, low = 0, high = 4

Iteration 1: mid = (0+4)/2 = 2 k(mid) = k (2) = 33

Now key > k (mid)

Thus low = mid + 1 = 3

Iteration 2: low = 3, high = 4 (low <= high)

Mid = 3+4 / 2 = 3.5 ~ 3 (integer value)

 Here key > k (mid)

Thus low = 3+1 = 4

Iteration 3: low = 4, high = 4 (low<= high)

Mid = (4+4)/2 = 4

Here key = k(mid)

Thus, the record is at mid+1 position that means 5


Related Discussions:- Example of binary search

Construct a minimum spanning tree, Construct G for α, n, and W given as com...

Construct G for α, n, and W given as command line parameters. Throw away edges that have an asymmetric relation between nodes. That is, if A is connected to B, but B is not connect

Example of single node with multiple elements, The class Element represents...

The class Element represents a single node that can be part of multiple elements on a hotplate and runs in its own thread. The constructor accepts the initial temperature and a hea

Techniques of representing polynomials using arrays, Q. Explain any three m...

Q. Explain any three methods or techniques of representing polynomials using arrays. Write which method is most efficient or effective for representing the following polynomials.

Different ways for representing s graph, W h at are the different ways by...

W h at are the different ways by which we can represent graph?  Represent the graph drawn below using those ways.     T he d iff e r e nt w a y s by

Tree, tree is graph or not

tree is graph or not

Array, how to define the size of array

how to define the size of array

Explain floyds algorithm, Explain Floyd's algorithm It is convenient to...

Explain Floyd's algorithm It is convenient to record the lengths of shortest paths in an n by n matrix D known as the  distance matrix: the element d ij   in the i th   row an

Circular doubly link list, what is circular doubly link list?write down the...

what is circular doubly link list?write down the algorithm for insertion of elements in circular doubly link list

Explain the concept of hidden lines and surface removal, Explain the concep...

Explain the concept of hidden lines The problem of hidden lines or surfaces was implicit even in 2-D graphics, but we did not mention it there, because what was intended to be

Explain about the abstract data type, Explain about the Abstract data type ...

Explain about the Abstract data type Abstract data type (ADT) A set of values (the carrier set) and operations on those values

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