SlideShare a Scribd company logo
1 of 33
Download to read offline
GVK CHINMAYA VIDYALAYA
SENIOR SECONDARY SCHOOL
Kothuru, Indukurupet, SPS Nellore
Binary Search
Class: 12 Subject: Python Teacher: C Vijaya Kumar
B.Tech,MBA
2 3 4 10 40
high>=low:
4>=0
TRUE
mid=2
LOW HIGHMID
2 3 4 10 40
If arr[mid]==10:
arr[2]==10:
4 = 10
FALSE
LOW HIGHMID
2 3 4 10 40
Elif arr[mid]>x:
arr[2]>10:
4>10:
FALSE
LOW HIGHMID
2 3 4 10 40
binary(arr,3,4,10)
LOW HIGH
2 3 4 10 40
binary(arr,3,4,10)
If high>=low:
4>=3:
TRUE
mid=3
LOW,MID HIGH
2 3 4 10 40
arr[3]==10
TRUE
LOW and MID HIGH
2 3 4 10 40
Mid value is
returned here
LOW and MID HIGH
2 3 4 10 40
result=3
LOW and MID HIGH
2 3 4 10 40
If 3 not equal to -1:
Item found
LOW and MID HIGH
Lets us see the flow of execution of item not found in the same binary search
2 3 4 10 40
HIGHLOW
def binary(arr,0,4,41):
2 3 4 10 40
HIGHLOW
If 4>=0:
TRUE
mid=2
MID
2 3 4 10 40
HIGHLOW
If 4==41:
False
MID
2 3 4 10 40
HIGHLOW
elif 4 > 41:
False
MID
2 3 4 10 40
HIGHLOW
binary(arr,3,4,41):
MID
2 3 4 10 40
HIGHLOW
binary(arr,3,4,41):
2 3 4 10 40
HIGHLOW and MID
If 4 > 3:
TRUE
mid=3
2 3 4 10 40
HIGHLOW and MID
elif 10 > 41:
FALSE
2 3 4 10 40
HIGHLOW and MID
binary(arr,4,4,41)
2 3 4 10 40
LOW and HIGH
binary(arr,4,4,41)
2 3 4 10 40
MID,LOW and HIGH
If 4 >= 4:
TRUE
Mid = 4
2 3 4 10 40
MID,LOW and HIGH
if 40 == 41:
FALSE
2 3 4 10 40
MID,LOW and HIGH
elif 40 > 41:
FALSE
2 3 4 10 40
MID,LOW and HIGH
binary (arr,5,4,41):
2 3 4 10 40
HIGH
binary (arr,5,4,41):
Low = 5
High = 4
X = 41
2 3 4 10 40
HIGH
If 4>=5:
FALSE
2 3 4 10 40
HIGH
Return -1
2 3 4 10 40
HIGH
Return -1 (Third Recursive Call)
2 3 4 10 40
HIGH
Return -1 (Second recursive call)
2 3 4 10 40
HIGH
Return -1 (First recursive call)
2 3 4 10 40
HIGH
Result = -1
2 3 4 10 40
HIGH
If False:
Else
print(“element not present in array”)

More Related Content

What's hot (13)

squares and square roots
squares and square rootssquares and square roots
squares and square roots
 
Chapter 3 Solving By Graph
Chapter 3 Solving By GraphChapter 3 Solving By Graph
Chapter 3 Solving By Graph
 
Integers
IntegersIntegers
Integers
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Integers slideshow
Integers slideshowIntegers slideshow
Integers slideshow
 
Multipying integers
Multipying integersMultipying integers
Multipying integers
 
Add-subtract of integers
Add-subtract of integersAdd-subtract of integers
Add-subtract of integers
 
Integers and Operation of Integers
Integers and Operation of IntegersIntegers and Operation of Integers
Integers and Operation of Integers
 
Integers
IntegersIntegers
Integers
 
Integers
IntegersIntegers
Integers
 
Integers
Integers Integers
Integers
 
Squaring of numbers
Squaring of numbersSquaring of numbers
Squaring of numbers
 
Topic
TopicTopic
Topic
 

More from chinthala Vijaya Kumar

GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...
GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...
GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...chinthala Vijaya Kumar
 
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12chinthala Vijaya Kumar
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21chinthala Vijaya Kumar
 
Inserting an element in a sorted array using traditional algorithm
Inserting an element in a sorted array using traditional algorithmInserting an element in a sorted array using traditional algorithm
Inserting an element in a sorted array using traditional algorithmchinthala Vijaya Kumar
 

More from chinthala Vijaya Kumar (11)

GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...
GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...
GVKCV Computer Science(083) Pre board sample paper 2 Class 12 (20-21) with so...
 
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
Computer Science(083) Python Pre Board Exam 1 Sample Paper Class 12
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
 
Inserting an element in a sorted array using traditional algorithm
Inserting an element in a sorted array using traditional algorithmInserting an element in a sorted array using traditional algorithm
Inserting an element in a sorted array using traditional algorithm
 
Linear search python CBSE Class 12
Linear search python CBSE Class 12Linear search python CBSE Class 12
Linear search python CBSE Class 12
 
Data visualization pyplot
Data visualization pyplotData visualization pyplot
Data visualization pyplot
 
Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11Data handling CBSE PYTHON CLASS 11
Data handling CBSE PYTHON CLASS 11
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Python Fundamentals Class 11
Python Fundamentals Class 11Python Fundamentals Class 11
Python Fundamentals Class 11
 
File handling CBSE CLASS 12
File handling CBSE CLASS 12File handling CBSE CLASS 12
File handling CBSE CLASS 12
 
Recursion CBSE Class 12
Recursion CBSE Class 12Recursion CBSE Class 12
Recursion CBSE Class 12
 

Recently uploaded

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfElizabeth Walsh
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 

Recently uploaded (20)

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

Binary search in Python using recursion

  • 1. GVK CHINMAYA VIDYALAYA SENIOR SECONDARY SCHOOL Kothuru, Indukurupet, SPS Nellore Binary Search Class: 12 Subject: Python Teacher: C Vijaya Kumar B.Tech,MBA
  • 2. 2 3 4 10 40 high>=low: 4>=0 TRUE mid=2 LOW HIGHMID
  • 3. 2 3 4 10 40 If arr[mid]==10: arr[2]==10: 4 = 10 FALSE LOW HIGHMID
  • 4. 2 3 4 10 40 Elif arr[mid]>x: arr[2]>10: 4>10: FALSE LOW HIGHMID
  • 5. 2 3 4 10 40 binary(arr,3,4,10) LOW HIGH
  • 6. 2 3 4 10 40 binary(arr,3,4,10) If high>=low: 4>=3: TRUE mid=3 LOW,MID HIGH
  • 7. 2 3 4 10 40 arr[3]==10 TRUE LOW and MID HIGH
  • 8. 2 3 4 10 40 Mid value is returned here LOW and MID HIGH
  • 9. 2 3 4 10 40 result=3 LOW and MID HIGH
  • 10. 2 3 4 10 40 If 3 not equal to -1: Item found LOW and MID HIGH
  • 11. Lets us see the flow of execution of item not found in the same binary search
  • 12. 2 3 4 10 40 HIGHLOW def binary(arr,0,4,41):
  • 13. 2 3 4 10 40 HIGHLOW If 4>=0: TRUE mid=2 MID
  • 14. 2 3 4 10 40 HIGHLOW If 4==41: False MID
  • 15. 2 3 4 10 40 HIGHLOW elif 4 > 41: False MID
  • 16. 2 3 4 10 40 HIGHLOW binary(arr,3,4,41): MID
  • 17. 2 3 4 10 40 HIGHLOW binary(arr,3,4,41):
  • 18. 2 3 4 10 40 HIGHLOW and MID If 4 > 3: TRUE mid=3
  • 19. 2 3 4 10 40 HIGHLOW and MID elif 10 > 41: FALSE
  • 20. 2 3 4 10 40 HIGHLOW and MID binary(arr,4,4,41)
  • 21. 2 3 4 10 40 LOW and HIGH binary(arr,4,4,41)
  • 22. 2 3 4 10 40 MID,LOW and HIGH If 4 >= 4: TRUE Mid = 4
  • 23. 2 3 4 10 40 MID,LOW and HIGH if 40 == 41: FALSE
  • 24. 2 3 4 10 40 MID,LOW and HIGH elif 40 > 41: FALSE
  • 25. 2 3 4 10 40 MID,LOW and HIGH binary (arr,5,4,41):
  • 26. 2 3 4 10 40 HIGH binary (arr,5,4,41): Low = 5 High = 4 X = 41
  • 27. 2 3 4 10 40 HIGH If 4>=5: FALSE
  • 28. 2 3 4 10 40 HIGH Return -1
  • 29. 2 3 4 10 40 HIGH Return -1 (Third Recursive Call)
  • 30. 2 3 4 10 40 HIGH Return -1 (Second recursive call)
  • 31. 2 3 4 10 40 HIGH Return -1 (First recursive call)
  • 32. 2 3 4 10 40 HIGH Result = -1
  • 33. 2 3 4 10 40 HIGH If False: Else print(“element not present in array”)