SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Binary Search
Illustrated walk through
Iterative binary search
int begin = 0;
int last = array.Length - 1;
int mid = 0;
while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

Part #1 Initialize pointers

Part #2 Search
begin

last

mid
x

4

This is what we
search for

[0]

[1]

[2]

[3]

2

4

5

6

Let’s look for 4
begin

0

x
last

mid

3

0
[0]

[1]

[2]

[3]

2

4

5

6

int begin = 0;
int last = array.Length - 1;
int mid = 0;

4
begin

0

x

0 <= 3
is true
mid

last

3

0
[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4
begin

0

x
last

(0+3)/2 = 1

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4
begin

0

x
last
mid

4

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4<4
is false
begin

0

x
last
mid

4

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if ( array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4>4
is false
begin

0

x
last
mid

4

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}

We found x=4 at
index 1!

return -1;
begin

last

mid
x

5

This is what we
search for

[0]

[1]

[2]

[3]

2

4

5

6

Let’s look for 5
begin

0

x
last

mid

3

0
[0]

[1]

[2]

[3]

2

4

5

6

int begin = 0;
int last = array.Length - 1;
int mid = 0;

5
begin

0

x

0 <= 3
is true
mid

last

3

0
[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5
begin

0

x
last

(0+3)/2 = 1

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5
begin

0

x
last
mid

5

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4<5
is true
begin

2

x
last

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5
begin

2 <= 3
is true

2

x
last

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5
begin

2

x
last

(2+3)/2 =2

mid

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5
begin

2

x
last

mid

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5

5<5
is false
begin

2

x
last

mid

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if ( array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

5

5>5
is false
begin

2

x
last

mid

5

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}

We found x=5 at
index 2!

return -1;
begin

last

mid
x

6

This is what we
search for

[0]

[1]

[2]

[3]

2

4

5

6

Let’s look for 6
begin

0

x
last

mid

3

0
[0]

[1]

[2]

[3]

2

4

5

6

int begin = 0;
int last = array.Length - 1;
int mid = 0;

6
begin

0

x

0 <= 3
is true
mid

last

3

0
[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6
begin

0

x
last

(0+3)/2 = 1

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6
begin

0

x
last
mid

6

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

4<6
is true
begin

2

x
last

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6
begin

2 <= 3
is true

2

x
last

mid

3

1

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6
begin

2

x
last

(2+3)/2 =2

mid

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6
begin

2

x
last

mid

3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

6

5<6
is true
begin
last
mid

3
3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

x

6
begin
last

3 <= 3
is true
mid

3
3

2

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

x

6
begin
last

3

mid

(3+3)/2 = 3

3

3

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

x

6
begin

3

last

3

mid

3

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

x

6

6<6
is false
begin

3

last

3

mid

3

[0]

[1]

[2]

[3]

2

4

5

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if ( array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}
return -1;

x

6

6>6
is false
begin

3

last

3

mid

3

[0]

[1]

[2]

4

5

6

[3]

2

x

6

while (begin <= last) {
mid = (begin + last) / 2;
if (array[mid] < x) {
begin = mid + 1;
}
else if (array[mid] > x) {
last = mid - 1;
}
else {
return mid;
}
}

We found x=6 at
index 3!

return -1;

Contenu connexe

Tendances

Maths vegas functions_review
Maths vegas functions_reviewMaths vegas functions_review
Maths vegas functions_review
JJkedst
 
11 x1 t09 03 rules for differentiation (2013)
11 x1 t09 03 rules for differentiation (2013)11 x1 t09 03 rules for differentiation (2013)
11 x1 t09 03 rules for differentiation (2013)
Nigel Simmons
 
6.1 & 6.4 an overview of the area problem area
6.1 & 6.4 an overview of the area problem area6.1 & 6.4 an overview of the area problem area
6.1 & 6.4 an overview of the area problem area
dicosmo178
 

Tendances (17)

ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-ゲーム理論BASIC 演習4 -交渉集合を求める-
ゲーム理論BASIC 演習4 -交渉集合を求める-
 
ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-ゲーム理論BASIC 演習6 -仁を求める-
ゲーム理論BASIC 演習6 -仁を求める-
 
The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184The Ring programming language version 1.5.3 book - Part 69 of 184
The Ring programming language version 1.5.3 book - Part 69 of 184
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
24 order of operations
24 order of operations24 order of operations
24 order of operations
 
Maths vegas functions_review
Maths vegas functions_reviewMaths vegas functions_review
Maths vegas functions_review
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter two
 
4 order of operations 125s
4 order of operations 125s4 order of operations 125s
4 order of operations 125s
 
23 order of operations
23 order of operations23 order of operations
23 order of operations
 
Ken20150417
Ken20150417Ken20150417
Ken20150417
 
Program Language - Fall 2013
Program Language - Fall 2013 Program Language - Fall 2013
Program Language - Fall 2013
 
solucionario de purcell 1
solucionario de purcell 1solucionario de purcell 1
solucionario de purcell 1
 
Algebra 6
Algebra 6Algebra 6
Algebra 6
 
11 x1 t09 03 rules for differentiation (2013)
11 x1 t09 03 rules for differentiation (2013)11 x1 t09 03 rules for differentiation (2013)
11 x1 t09 03 rules for differentiation (2013)
 
6.1 & 6.4 an overview of the area problem area
6.1 & 6.4 an overview of the area problem area6.1 & 6.4 an overview of the area problem area
6.1 & 6.4 an overview of the area problem area
 
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
ゲーム理論BASIC 第19回 -有限回繰り返しゲーム-
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 

Similaire à Binary search: illustrated step-by-step walk through

AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
Ankit Gupta
 
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
Kanahaiya Gupta
 
Question 1,2,4 ------------------------------------Please check.pdf
Question 1,2,4 ------------------------------------Please check.pdfQuestion 1,2,4 ------------------------------------Please check.pdf
Question 1,2,4 ------------------------------------Please check.pdf
anandhomeneeds
 

Similaire à Binary search: illustrated step-by-step walk through (6)

Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
 
Ds sorting
Ds sortingDs sorting
Ds sorting
 
Coordinates-Midpoint-Bingo-M.pptx
Coordinates-Midpoint-Bingo-M.pptxCoordinates-Midpoint-Bingo-M.pptx
Coordinates-Midpoint-Bingo-M.pptx
 
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
Prefix Sum Algorithm | Prefix Sum Array Implementation | EP2
 
Question 1,2,4 ------------------------------------Please check.pdf
Question 1,2,4 ------------------------------------Please check.pdfQuestion 1,2,4 ------------------------------------Please check.pdf
Question 1,2,4 ------------------------------------Please check.pdf
 

Plus de Yoshi Watanabe

Plus de Yoshi Watanabe (7)

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptx
 
Git フェッチ
Git フェッチGit フェッチ
Git フェッチ
 
Git リベース
Git リベースGit リベース
Git リベース
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクト
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthrough
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthrough
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Binary search: illustrated step-by-step walk through