SlideShare ist ein Scribd-Unternehmen logo
Sample Question:
Write a Python program to implement a binary search algorithm to
search for a given number in a sorted list.
Solution:
Explanation:
The binary search algorithm is used to search for a given number in
a sorted list. It works by dividing the list in half at each step, and
then searching only in the half of the list where the target number
could be. This reduces the search time significantly compared to a
linear search algorithm, which searches the entire list one element
at a time.
The solution above is a Python program that implements the binary
search algorithm. The program takes a sorted list 'arr', left and right
indices of the list, and the target number 'num' as inputs. It then
recursively divides the list in half until it finds the target number or
determines that the number is not in the list. Finally, it returns the
index of the target number or -1 if the number is not found.
def binary_search(arr, left, right, num):
if right >= left:
mid = left + (right - left) // 2
if arr[mid] == num:
return mid
elif arr[mid] > num:
return binary_search(arr, left, mid - 1, num)
else:
return binary_search(arr, mid + 1, right, num)
else:
return -1
arr = [2, 3, 4, 10, 40]
num = 10
result = binary_search(arr, 0, len(arr) - 1, num)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")

Weitere Àhnliche Inhalte

Ähnlich wie PROGRAMMING HOMEWORK HELP

Searching
Searching Searching
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Algorithms Intro Lecture
Algorithms Intro LectureAlgorithms Intro Lecture
Algorithms Intro Lecture
Ira D
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Searching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdfSearching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdf
AkshatMehrotra14
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Binary Search.pptx
Binary Search.pptxBinary Search.pptx
Binary Search.pptx
syedjafer8
 
Given a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docxGiven a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docx
rtodd751
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
NilaNila16
 
Chapter 14
Chapter 14Chapter 14
Chapter 14Terry Yoast
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
chauhankapil
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++poonam.rwalia
 
L1803016468
L1803016468L1803016468
L1803016468
IOSR Journals
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
SSN College of Engineering, Kalavakkam
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
manojmanoj218596
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
enidcruz
 

Ähnlich wie PROGRAMMING HOMEWORK HELP (20)

Searching
Searching Searching
Searching
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Algorithms Intro Lecture
Algorithms Intro LectureAlgorithms Intro Lecture
Algorithms Intro Lecture
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Searching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdfSearching, Sorting and Complexity analysis in DS.pdf
Searching, Sorting and Complexity analysis in DS.pdf
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Binary Search.pptx
Binary Search.pptxBinary Search.pptx
Binary Search.pptx
 
Given a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docxGiven a list of numbers- count and return how many pairs there are- A.docx
Given a list of numbers- count and return how many pairs there are- A.docx
 
Recurrence Relation
Recurrence RelationRecurrence Relation
Recurrence Relation
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
L1803016468
L1803016468L1803016468
L1803016468
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
 

Mehr von Economics Homework Helper

Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
Economics Homework Helper
 
Unlock Your Academic Success with Economics Homework Helper! 📚✹
Unlock Your Academic Success with Economics Homework Helper! 📚✹Unlock Your Academic Success with Economics Homework Helper! 📚✹
Unlock Your Academic Success with Economics Homework Helper! 📚✹
Economics Homework Helper
 
Macroeconomics Homework Help
Macroeconomics Homework HelpMacroeconomics Homework Help
Macroeconomics Homework Help
Economics Homework Helper
 
Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
Economics Homework Helper
 
Monetary Homework Solution
Monetary Homework SolutionMonetary Homework Solution
Monetary Homework Solution
Economics Homework Helper
 
Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
Economics Homework Helper
 
Economics Homework Help & Writing Services.pptx
Economics Homework Help & Writing Services.pptxEconomics Homework Help & Writing Services.pptx
Economics Homework Help & Writing Services.pptx
Economics Homework Helper
 
Economics Homework Help.pptx
Economics Homework Help.pptxEconomics Homework Help.pptx
Economics Homework Help.pptx
Economics Homework Helper
 
Economics Homework Help.pptx
Economics Homework Help.pptxEconomics Homework Help.pptx
Economics Homework Help.pptx
Economics Homework Helper
 
Labour Economics.pptx
Labour Economics.pptxLabour Economics.pptx
Labour Economics.pptx
Economics Homework Helper
 
Public Economics Help & Writing Services.pptx
Public Economics Help & Writing Services.pptxPublic Economics Help & Writing Services.pptx
Public Economics Help & Writing Services.pptx
Economics Homework Helper
 
Microeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptxMicroeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptx
Economics Homework Helper
 
Macro Development.pptx
Macro Development.pptxMacro Development.pptx
Macro Development.pptx
Economics Homework Helper
 
Do My Economics Homework.pptx
Do My Economics Homework.pptxDo My Economics Homework.pptx
Do My Economics Homework.pptx
Economics Homework Helper
 
Microeconomics-Homework-Help.pptx
Microeconomics-Homework-Help.pptxMicroeconomics-Homework-Help.pptx
Microeconomics-Homework-Help.pptx
Economics Homework Helper
 
Public Economics.pptx
Public Economics.pptxPublic Economics.pptx
Public Economics.pptx
Economics Homework Helper
 
Do You Need macroeconomics Homework Help.pptx
Do You Need macroeconomics Homework Help.pptxDo You Need macroeconomics Homework Help.pptx
Do You Need macroeconomics Homework Help.pptx
Economics Homework Helper
 
Best Microeconomics Assignment Help by the Leading Experts.pptx
Best Microeconomics Assignment Help by the Leading Experts.pptxBest Microeconomics Assignment Help by the Leading Experts.pptx
Best Microeconomics Assignment Help by the Leading Experts.pptx
Economics Homework Helper
 
Public Economics | Economics Homework Helper.pptx
Public Economics | Economics Homework Helper.pptxPublic Economics | Economics Homework Helper.pptx
Public Economics | Economics Homework Helper.pptx
Economics Homework Helper
 
Economics Homework helper
Economics Homework helperEconomics Homework helper
Economics Homework helper
Economics Homework Helper
 

Mehr von Economics Homework Helper (20)

Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
 
Unlock Your Academic Success with Economics Homework Helper! 📚✹
Unlock Your Academic Success with Economics Homework Helper! 📚✹Unlock Your Academic Success with Economics Homework Helper! 📚✹
Unlock Your Academic Success with Economics Homework Helper! 📚✹
 
Macroeconomics Homework Help
Macroeconomics Homework HelpMacroeconomics Homework Help
Macroeconomics Homework Help
 
Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
 
Monetary Homework Solution
Monetary Homework SolutionMonetary Homework Solution
Monetary Homework Solution
 
Economics Homework Help
Economics Homework HelpEconomics Homework Help
Economics Homework Help
 
Economics Homework Help & Writing Services.pptx
Economics Homework Help & Writing Services.pptxEconomics Homework Help & Writing Services.pptx
Economics Homework Help & Writing Services.pptx
 
Economics Homework Help.pptx
Economics Homework Help.pptxEconomics Homework Help.pptx
Economics Homework Help.pptx
 
Economics Homework Help.pptx
Economics Homework Help.pptxEconomics Homework Help.pptx
Economics Homework Help.pptx
 
Labour Economics.pptx
Labour Economics.pptxLabour Economics.pptx
Labour Economics.pptx
 
Public Economics Help & Writing Services.pptx
Public Economics Help & Writing Services.pptxPublic Economics Help & Writing Services.pptx
Public Economics Help & Writing Services.pptx
 
Microeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptxMicroeconomics-Help-Experts.pptx
Microeconomics-Help-Experts.pptx
 
Macro Development.pptx
Macro Development.pptxMacro Development.pptx
Macro Development.pptx
 
Do My Economics Homework.pptx
Do My Economics Homework.pptxDo My Economics Homework.pptx
Do My Economics Homework.pptx
 
Microeconomics-Homework-Help.pptx
Microeconomics-Homework-Help.pptxMicroeconomics-Homework-Help.pptx
Microeconomics-Homework-Help.pptx
 
Public Economics.pptx
Public Economics.pptxPublic Economics.pptx
Public Economics.pptx
 
Do You Need macroeconomics Homework Help.pptx
Do You Need macroeconomics Homework Help.pptxDo You Need macroeconomics Homework Help.pptx
Do You Need macroeconomics Homework Help.pptx
 
Best Microeconomics Assignment Help by the Leading Experts.pptx
Best Microeconomics Assignment Help by the Leading Experts.pptxBest Microeconomics Assignment Help by the Leading Experts.pptx
Best Microeconomics Assignment Help by the Leading Experts.pptx
 
Public Economics | Economics Homework Helper.pptx
Public Economics | Economics Homework Helper.pptxPublic Economics | Economics Homework Helper.pptx
Public Economics | Economics Homework Helper.pptx
 
Economics Homework helper
Economics Homework helperEconomics Homework helper
Economics Homework helper
 

KĂŒrzlich hochgeladen

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
KrisztiĂĄn SzĂĄraz
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Lapbook sobre os Regimes TotalitĂĄrios.pdf
Lapbook sobre os Regimes TotalitĂĄrios.pdfLapbook sobre os Regimes TotalitĂĄrios.pdf
Lapbook sobre os Regimes TotalitĂĄrios.pdf
Jean Carlos Nunes PaixĂŁo
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

KĂŒrzlich hochgeladen (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Lapbook sobre os Regimes TotalitĂĄrios.pdf
Lapbook sobre os Regimes TotalitĂĄrios.pdfLapbook sobre os Regimes TotalitĂĄrios.pdf
Lapbook sobre os Regimes TotalitĂĄrios.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

PROGRAMMING HOMEWORK HELP

  • 1.
  • 2. Sample Question: Write a Python program to implement a binary search algorithm to search for a given number in a sorted list. Solution: Explanation: The binary search algorithm is used to search for a given number in a sorted list. It works by dividing the list in half at each step, and then searching only in the half of the list where the target number could be. This reduces the search time significantly compared to a linear search algorithm, which searches the entire list one element at a time. The solution above is a Python program that implements the binary search algorithm. The program takes a sorted list 'arr', left and right indices of the list, and the target number 'num' as inputs. It then recursively divides the list in half until it finds the target number or determines that the number is not in the list. Finally, it returns the index of the target number or -1 if the number is not found.
  • 3. def binary_search(arr, left, right, num): if right >= left: mid = left + (right - left) // 2 if arr[mid] == num: return mid elif arr[mid] > num: return binary_search(arr, left, mid - 1, num) else: return binary_search(arr, mid + 1, right, num) else: return -1 arr = [2, 3, 4, 10, 40] num = 10 result = binary_search(arr, 0, len(arr) - 1, num) if result != -1: print("Element is present at index", result) else: print("Element is not present in array")