codinasion-archive / codinasion-monorepo

Community Monorepo
https://codinasion.org
MIT License
52 stars 171 forks source link

Implement Binary Search #5192

Open harshraj8843 opened 6 months ago

harshraj8843 commented 6 months ago

Description

Write a 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
### Tracking Issues
- [ ] #5194
- [ ] #5195
- [ ] #5196
- [ ] #5197
- [ ] #5198
- [ ] #5199
- [ ] #5200
- [ ] #5201
- [ ] #5202
- [ ] #5203
- [ ] #5204
- [ ] #5205
- [ ] #5206
- [ ] #5207
- [ ] #5208
- [ ] #5209
- [ ] #5210
- [ ] #5211
- [ ] #5212
- [ ] #5213
codinasion-bot[bot] commented 6 months ago

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

vherak commented 2 weeks ago

!assign