Learn how to manipulate data in algorithm binary search.

© All Rights Reserved

25 views

Learn how to manipulate data in algorithm binary search.

© All Rights Reserved

- Excel Functions
- Salut Macro
- swigR2
- Excel Dictionary
- A Com Put Able Algorithm for Ensuring Segregation in a Complex Piping Network
- Germany.pdf
- Excel Function List
- Cs301 02 Mid Spring 20101 My Comp Let File
- DAA
- Two Dimensional Arrays
- APS
- Chapter 8 Homework Solutions
- Arrays
- Chapter 8 Homework Solution1
- W5 Queueing Theory
- List__assign - C++ Reference
- cpsc lecture1
- StataMata.beamer.uksuG14
- LOGIX5000 Data Access
- Lecture9_Array_Part2 (1)

You are on page 1of 20

CS126

Searching

Binary Search

Binary Search

Sequential search is not efficient for large

lists as it searches half the list, on average

Another search algorithm Binary search

Very fast

But can only be performed on ordered lists

Example

If you are looking for you friends number in the

phone book, you may decide to look from half

way , you know the book is ordered

alphabetically therefore if you decide the name is

in the right half you can disregard the left half

throw it away just concentrate on the right half

this way your search is dramatically reduced

only have book o search do the same process

again until eventually u either find the name or

decide its not there binary search is the same

Divide & Conquer technique

When a list is sorted and we have

random access to the list as in an array

or vector implementation

we can take advantage of this

additional structure in our search

methods.

binary search algorithm uses the

Divide & Conquermethod to search

the list

Divide & Conquer technique

First the search item is compared with the

middle element of the list. If the search

item is less than the middle element of the

list, we restrict the search to the first half of

the list; otherwise we search the second

half of the list

Binary Search

Consider a sorted list of length 12

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

4 8 19 25 34 39 45 48 66 75 89 95

list

Binary Search

Suppose we want to find 75

Entire list is searched compare 75 with middle

element in list, list[5] (which is 39)

Because 75 Is in list [6]..list[11] we restrict

search there

4 8 19 25 34 39 45 48 66 75 89 95

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

list

Search list

MID

Binary Search

The process is now repeated on the list

[6]..list[11] which is a list of 6

4 8 19 25 34 39 45 48 66 75 89 95

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

list

Search list

Task - individually

During a binary search , which elements in

the array

4 8 12 14 20 24

Are compared to the target when the target

is a. 2 b.8 c.15

Answer

a. 12 and 4

B. 12, 4 and 8

C. 12, 20 and 14

most of the array is not searched at all ,

saving much time - thus BS algorithm is

very fast.

But how fast ? Counting the comparisons

I.e each time algorithm divides the array in

half - can provide us a measure of the

algorithms efficiency

Binary search Analysis

Suppose we are working with a sorted array

of n integers

Initially first = 0 and (because an array index in

J ava starts at 0 and n denotes the number of

elements in the list(length) then last = n-1

The element midway in an array indexed

from 0 to n-1 is mid = (n 1) / 2.

If the target is less than the value at mid,

then since the array is sorted we can be

certain that the target element is not in the

array from positions mid to n-1. In pseudo

code the idea is:

low = 0; high = n -1;

mid = (low + high) / 2;

if target = array[mid]

then return mid

else

if target < array[mid]

then target is not in range mid .. n-1

discard upper half by setting high = mid 1;

else

target is not in the 0 ..mid range;

discard lower half by setting low = mid + 1;

repeat with new range until possible array is of length 1.

If the lower half of the list 0..mid was discarded by the

above algorithm then the next probe would be at

mid = [(mid+1) + (n-1)] / 2

Binary search Analysis

which is the midpoint of the upper half.

Continuing in this way, unless we find the target, we

halve the list each time until it becomes of size one

and we either have found the target or it was not

present originally.

The complexity of such an algorithm the work

required to complete it given a list of size n initially

is the number of probes that are required. That is,

how many times can a list of size n be halved before

it becomes of size one? Let k be the smallest integer

such that

Binary search Analysis

n / 2^k = 1

Then k = ceiling (log n). Of course,

sometimes the target is found before k

probes. It has been proved that the average

time for a successful search by this method

is approximately log n 1 . So binary search

is O(log n).

publ i c i nt bi nar ySear ch( i nt t ar get ) {

i nt l ow = 0;

i nt hi gh = a. l engt h 1;

i nt mi d;

whi l e ( l ow <= hi gh) {

mi d = ( l ow + hi gh) / 2;

i f ( t ar get == a[ mi d] ) / / mat ch

r et ur n mi d;

el se

i f ( t ar get < a[ mi d] )

/ / sear ch l ow end of ar r ay

hi gh = mi d 1;

el se

/ / sear ch hi gh end of ar r ay

l ow = mi d + 1;

}

r et ur n 1;

}

Binary search Analysis

The above algorithm checks on each

iteration for whether target is at array[mid].

This is relatively unlikely until the sublists

become small

so a refinement of this algorithm involves

omitting that test until the final sublist (of

length one).

A sequential search of either a list, an array,

or a chain looks at the first item, the second

item, and so on until it either finds a

particular item or determines that the item

does not occur in the group

Average case of sequential search is O(n)

A binary search of an array requires that the

array be sorted . It looks first at the middle of

the array to determine in which half the

desired item can occur. The search repeats

this strategy on only this half of the array

Binary Search Vs Linear Search

The benefit of binary search over linear

search becomes significant for lists over

about 100 elements.

For smaller lists linear search may be faster

because of the speed of the simple

increment compared with the divisions

needed in binary search.

The general moral is that for large lists

binary search is very much faster than linear

search, but is not worth while for small lists.

Note that binary search is not appropriate

for linked list structures (no random access

for the middle term).

Demonstration

http://www.cosc.canterbury.ac.nz/peopl

e/mukundan/dsal/BSearch.html

in comparison linear search

http://www.cosc.canterbury.ac.nz/people/m

ukundan/dsal/LSearch.html

- Excel FunctionsUploaded bysiddharth_43293
- Salut MacroUploaded bylijika
- swigR2Uploaded byLi Ann
- Excel DictionaryUploaded bypcilops
- A Com Put Able Algorithm for Ensuring Segregation in a Complex Piping NetworkUploaded bylewist57
- Germany.pdfUploaded byWilliam Gómez Ortega
- Excel Function ListUploaded byIkshvaku Kansara
- Cs301 02 Mid Spring 20101 My Comp Let FileUploaded bySobia_Zaheer_2252
- DAAUploaded byanon_338138648
- Two Dimensional ArraysUploaded bySrivenu Chinna
- APSUploaded byAnusha Naidu
- Chapter 8 Homework SolutionsUploaded byJyotsna Talreja Wassan
- ArraysUploaded byDilip Naidu
- Chapter 8 Homework Solution1Uploaded byhbomber20
- W5 Queueing TheoryUploaded byAngel
- List__assign - C++ ReferenceUploaded byPranay Kumar
- cpsc lecture1Uploaded byAlbert Chang
- StataMata.beamer.uksuG14Uploaded byMichael Ray
- LOGIX5000 Data AccessUploaded bywilfredo
- Lecture9_Array_Part2 (1)Uploaded byPqa Afiqah
- 08 DS and Algorithm Session 11Uploaded byabiramihr
- subtraction algorithm annotated planning templateUploaded byapi-400542088
- Computer Science Field Guide - Student VersionUploaded bysaja
- Template ProgrammingUploaded byPrince Agyenim Boateng
- 02 Program Design.pdfUploaded byAgus Prasetyo
- Experiment No. 3Uploaded byDarrel Damo Pasalo
- BITCOIN.docxUploaded byarunkumar
- MIT6_094IAP10_lec04Uploaded bytombasingh
- dfdUploaded bydarwinvargas2011
- Tree_2Uploaded byShrey Bhadiyadara

- Basics of Recording NewUploaded byovimiky
- Imped an CIAUploaded byovimiky
- Stereo RecordingUploaded byovimiky
- Private InvestigationsUploaded byovimiky
- Recording Course OutlineUploaded byovimiky
- ProTools BasicUploaded byovimiky
- Learn How to Play the GuitarUploaded byEskay
- Mic TrainingUploaded byovimiky
- Basics of MixingUploaded bysipunk

- EAM-INT-325-TS-Document Management interface (7).docxUploaded byMounica Vendra
- Fifo DepthUploaded byAjayMP
- MJC JC2 H2 Maths Prelim P1 SolutionsUploaded byTan Eng Boon
- File-Management-in-SolidWorks.pdfUploaded byquiquichina17450
- Determining What is Personal Data Quick Reference GuideUploaded byfashaye
- MCF 2018 DAS Design Specification.pdfUploaded byLaura Villarreal
- Amcas Instruction ManualUploaded byBrad Isaacs
- Windows 10 Technical Preview [x64-x86] [ISO] - Joker_RETURNS Torrent - KickassTorrentsUploaded bynarendra239_qis
- 1 NASA EEE Parts Derating (1)Uploaded byJunior Alexander Marquina M
- Swift IOS AssessmentUploaded byimduser
- AWS MISTAKE Server Migration APIUploaded bydonna.nix
- Client-Server Databases.pdfUploaded byshylaja_p
- Arczar v. YelpUploaded byPriorSmart
- Change LogUploaded byKeegan
- welcomeCICSUploaded bysdineeshkumar
- Control Remoto RexrothUploaded byRaul E. Soli
- FEMA Technical Assistance CatalogUploaded byEddie
- IntroUploaded bypaladinooo
- networkUploaded byaniezahid
- Power System protection_PSTI_Load despatch_r1 .pptUploaded byRavikumar Kandukuri
- Researchpsvpwmaper Generation of Space Vector PWM Using Microcontroller Atmega 16Uploaded bymitul
- Main CoverUploaded byRubini Devi Mutthusamy
- Download Electrical Machines Books - PDF DriveUploaded byRasel Islam
- Himoinsa Profile 2013-EngUploaded bynicolasbastida
- Laser 3Uploaded byFarid Anuar
- HDLC_PPPUploaded byPusetso Mokitimi Rakotsoane
- Intellectual Property Rights Ppt Ks Doc 1Uploaded byUmasankar Chilumuri
- Satellite S55 A5279Uploaded byWilian Fabian Escandon
- As 4485.2-1997 Security for Health Care Facilities Procedures GuideUploaded bySAI Global - APAC
- Stack_18F4550Uploaded byChristopher Jenkins

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.