Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Binary search algorithm

Rating
-
Sold
-
Pages
6
Uploaded on
27-08-2022
Written in
2022/2023

Binary search algorithm explaining

Institution
Course

Content preview

Binary search algorithm
Definition

Search a sorted array by repeatedly dividing the search interval in half.
Begin with an interval covering the whole array. If the value of the search
key is less than the item in the middle of the interval, narrow the interval to
the lower half. Otherwise narrow it to the upper half. Repeatedly check
until the value is found or the interval is empty.

Generally, to find a value in unsorted array, we should look through
elements of an array one by one, until searched value is found. In case of
searched value is absent from array, we go through all elements. In
average, complexity of such an algorithm is proportional to the length of
the array.

Divide in half

A fast way to search a sorted array is to use a binary search. The idea is to
look at the element in the middle. If the key is equal to that, the search is
finished. If the key is less than the middle element, do a binary search on
the first half. If it's greater, do a binary search of the second half.

Performance

The advantage of a binary search over a linear search is astounding for
large numbers. For an array of a million elements, binary search, O(log N),
will find the target element with a worst case of only 20 comparisons.

Linear search, O(N), on average will take 500,000 comparisons to find the
element.

Algorithm

Algorithm is quite simple. It can be done either recursively or iteratively:

1. get the middle element;
2. if the middle element equals to the searched value, the algorithm stops;
3. otherwise, two cases are possible:
o searched value is less, than the middle element. In this case, go to

the step 1 for the part of the array, before middle element.
o searched value is greater, than the middle element. In this case, go

to the step 1 for the part of the array, after middle element.

, Now, we should define when iterations should stop?

First case is when searched element is found.

Second one is when subarray has no elements.

In this case,

We can conclude, that searched value doesn't present in the array.



Illustration of Binary search


• X[12]:




• Search for b=12

• mid = (0+11)/2 = 5. Compare b with X[5]: 12<20.

• So search in left half X[0..4]




• mid = (0+4)/2 = 2. Compare b with X[2]: 12 > 7.

• So search right half X[3..4]




 mid = (3+4)/2 = 3.Compare b with X[3]: b=X[3]=12.
 Return 3.

Written for

Institution
Course

Document information

Uploaded on
August 27, 2022
Number of pages
6
Written in
2022/2023
Type
SUMMARY

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
bakATD

Get to know the seller

Seller avatar
bakATD Chamberlain College Of Nursing
Follow You need to be logged in order to follow users or courses
Sold
4
Member since
3 year
Number of followers
1
Documents
1099
Last sold
1 month ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions