Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Write a Perl program to implement binary search #5205

Closed
harshraj8843 opened this issue Jan 12, 2024 · 2 comments · Fixed by #5664
Closed

Write a Perl program to implement binary search #5205

harshraj8843 opened this issue Jan 12, 2024 · 2 comments · Fixed by #5664
Assignees
Labels
closed closed issues/PRs good first issue Good for newcomers Perl perl related program

Comments

@harshraj8843
Copy link
Contributor

Description

Write a Perl program to implement binary search

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub-array as well until the size of the subarray reduces to zero.

Pseudocode

procedure binary_search
   A ← sorted array
   n ← size of array
   x ← value to be searched

   Set lowerBound = 1
   Set upperBound = n

   while x not found
      if upperBound < lowerBound
         EXIT: x does not exists.
   
      set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
      
      if A[midPoint] < x
         set lowerBound = midPoint + 1
         
      if A[midPoint] > x
         set upperBound = midPoint - 1 

      if A[midPoint] = x 
         EXIT: x found at location midPoint
   end while
   
end procedure

Example

list = [1,2,3,4,5]
value = 4

Output : 3
How to contribute
  • Comment !assign to assign this issue to yourself
  • Fork this repository
  • Create a new branch
  • Save the solution in program/program/implement-binary-search/implement_binary_search.pl
  • Commit the changes
  • Create a pull request
@harshraj8843 harshraj8843 added good first issue Good for newcomers Perl perl related program labels Jan 12, 2024
@codinasion-bot
Copy link

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

@codinasion-bot codinasion-bot bot added the triage Waiting for review label Jan 12, 2024
@Riyazul555
Copy link
Contributor

!assign

@codinasion-bot codinasion-bot bot added closed closed issues/PRs and removed triage Waiting for review labels May 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
closed closed issues/PRs good first issue Good for newcomers Perl perl related program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants