This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 307
Add Algorithm Binary Search #1172
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Binary search | ||
|
||
Binary search is a search algorithm that finds the position of a target value within a sorted array. | ||
|
||
## Example | ||
|
||
![Binary search](https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png) | ||
|
||
The above illustration shows the working of the binary search algorithm on a sorted array when the target value is **4**. | ||
|
||
## Algorithm | ||
|
||
Binary search works on sorted arrays. A binary search begins by comparing the middle element of the array with the target value. If the target value matches the middle element, its position in the array is returned. If the target value is less or more than the middle element, the search continues the lower or upper half of the array respectively with a new middle element, eliminating the other half from consideration. | ||
|
||
The pseudocode for binary search algorithm is as follows: | ||
|
||
``` | ||
BinarySearch(A[0..N-1], value) { | ||
low = 0 | ||
high = N - 1 | ||
while (low <= high) { | ||
// invariants: value > A[i] for all i < low | ||
value < A[i] for all i > high | ||
mid = (low + high) / 2 | ||
if (A[mid] > value) | ||
high = mid - 1 | ||
else if (A[mid] < value) | ||
low = mid + 1 | ||
else | ||
return mid | ||
} | ||
return not_found // value would be inserted at index "low" | ||
} | ||
``` | ||
|
||
## Complexity | ||
|
||
``` | ||
Worst case performance: O(log n) | ||
|
||
Best case performance: O(1) | ||
|
||
Average case performance: O(log n) | ||
|
||
Worst case space complexity: O(1) for iterative; O(log n) for recursive. | ||
``` | ||
|
||
## C++ Implementation | ||
|
||
```c++ | ||
int binarySearch(int arr[], int value, int left, int right) { | ||
int middle; | ||
while (left <= right) { | ||
middle = (left + right) / 2; | ||
if (arr[middle] == value) | ||
return middle; | ||
else if (arr[middle] > value) | ||
right = middle - 1; | ||
else | ||
left = middle + 1; | ||
} | ||
return -1; | ||
} | ||
``` | ||
|
||
:rocket: [Run Code](https://repl.it/CWZq/158) | ||
|
||
## Python Implementation | ||
|
||
```python | ||
def binary_search(l, value): | ||
low = 0 | ||
high = len(l)-1 | ||
while low <= high: | ||
mid = (low+high)//2 | ||
if l[mid] > value: high = mid-1 | ||
elif l[mid] < value: low = mid+1 | ||
else: return mid | ||
return -1 | ||
``` | ||
|
||
:rocket: [Run Code](https://repl.it/CWZi/2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its positionit**'**s positionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@atjonathan: Quoting, "Its, without an apostrophe, is the possessive of the pronoun it. It’s, with an apostrophe, is a contraction of it is or it has. If you’re not sure which spelling to use, try replacing it with it is or it has. If neither of those phrases works in its place, then its is the word you’re looking for.".
Sorry for the "Grammar Nazi" behavior 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dakshshah96, no worries 😜, i see the context now.