-
-
Notifications
You must be signed in to change notification settings - Fork 155
Added Binary Search Topic #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajay-dhangar
merged 8 commits into
codeharborhub:main
from
SadafKausar2025:binarysearch
Jun 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dda888b
Added Binary Search Topic
SadafKausar2025 134b571
Update Iterative_binary_search.md
ajay-dhangar 0815ea2
corrected
SadafKausar2025 526d829
Update 0017-letter-combinations-of-a-phone-number.md
ajay-dhangar 4326dc4
Update recursive_binary_search.md
ajay-dhangar 9ae92b6
Update binary_search.md
ajay-dhangar efee848
Update recursive_binary_search.md
ajay-dhangar 6dc95df
Update Iterative_binary_search.md
ajay-dhangar 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 hidden or 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,135 @@ | ||
--- | ||
id: iterative-binary-search-DSA | ||
title: Iterative Binary Search | ||
sidebar_label: Iterative Binary Search | ||
sidebar_position: 7 | ||
description: "In this blog post, we'll explore the iterative binary search algorithm, a fundamental technique in computer science for efficiently finding an element in a sorted array. You'll learn what iterative binary search is, how it works, and its time complexity. We'll also cover practical applications and common problems you can solve using this algorithm. By the end, you'll have a thorough understanding of iterative binary search and how to implement it in your programming projects." | ||
tags: [dsa, algorithms, binary search, iterative] | ||
--- | ||
|
||
Welcome, eager learner! Today, we embark on an enlightening journey through the world of iterative binary search. This powerful algorithm is essential for efficiently finding elements in sorted arrays, making it a staple in the toolkit of any adept programmer. Whether you're optimizing search operations or solving complex algorithmic challenges, understanding iterative binary search is crucial. Let's delve into its mechanics, applications, and implementation. | ||
|
||
## What is Iterative Binary Search? | ||
|
||
Iterative binary search is a highly efficient algorithm used to find an element in a sorted array. It works by repeatedly dividing the search interval in half, using an iterative approach. If the value of the search key is less than the item in the middle of the interval, the algorithm narrows the interval to the lower half. Otherwise, it narrows it to the upper half. The process continues until the search key is found or the interval is empty. | ||
|
||
In pseudo-code, iterative binary search is defined as follows: | ||
|
||
```cpp | ||
FUNCTION iterativeBinarySearch(array, key): | ||
low = 0 | ||
high = array.length - 1 | ||
WHILE low <= high: | ||
mid = (low + high) / 2 | ||
IF array[mid] == key: | ||
RETURN mid | ||
ELSE IF array[mid] < key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid - 1 | ||
RETURN -1 | ||
``` | ||
|
||
```cpp | ||
int iterativeBinarySearch(int array[], int size, int key) { | ||
int low = 0; | ||
int high = size - 1; | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
if (array[mid] == key) { | ||
return mid; | ||
} else if (array[mid] < key) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
``` | ||
|
||
How Iterative Binary Search Works | ||
Step-by-Step Explanation | ||
Initialize: Set two pointers, low at the beginning and high at the end of the array. | ||
Middle Element: Calculate the middle element's index. | ||
Comparison: | ||
If the middle element is the target, return its index. | ||
If the middle element is less than the target, discard the left half by setting low to mid + 1. | ||
If the middle element is greater than the target, discard the right half by setting high to mid - 1. | ||
Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer. | ||
Time Complexity | ||
The time complexity of iterative binary search is | ||
𝑂 | ||
( | ||
log | ||
| ||
𝑛 | ||
) | ||
O(logn), where | ||
𝑛 | ||
n is the number of elements in the array. This logarithmic time complexity makes iterative binary search significantly faster than linear search for large datasets. | ||
|
||
Practical Applications | ||
Iterative binary search is widely used in various real-world applications and algorithmic problems: | ||
|
||
1. Searching in a Sorted Array | ||
The primary use of iterative binary search is to find elements in a sorted array efficiently. It is the foundation for more complex search algorithms. | ||
|
||
2. Dictionary Lookups | ||
Iterative binary search is used in dictionaries (like the one you're reading now) to quickly find words and their definitions. | ||
|
||
3. Binary Search Trees | ||
Iterative binary search is the basis for searching in binary search trees (BSTs), a fundamental data structure in computer science. | ||
|
||
4. Finding Boundaries | ||
Iterative binary search can be adapted to find the first or last occurrence of a target element, making it useful in problems requiring boundary searches. | ||
|
||
Common Problems Solved Using Iterative Binary Search | ||
Iterative binary search can be adapted in various ways to solve different types of problems. Here are a couple of common problems: | ||
|
||
1. Lower Bound and Upper Bound | ||
These variations of iterative binary search are used to find the first and last occurrence of a target element in a sorted array. | ||
|
||
Lower Bound Pseudo-Code: | ||
|
||
```cpp | ||
FUNCTION lowerBound(array, key): | ||
low = 0 | ||
high = array.length | ||
WHILE low < high: | ||
mid = (low + high) / 2 | ||
IF array[mid] < key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid | ||
RETURN low | ||
|
||
``` | ||
|
||
Upper Bound Pseudo-Code: | ||
|
||
```cpp | ||
FUNCTION upperBound(array, key): | ||
low = 0 | ||
high = array.length | ||
WHILE low < high: | ||
mid = (low + high) / 2 | ||
IF array[mid] <= key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid | ||
RETURN low | ||
|
||
|
||
``` | ||
|
||
2. Rotated Sorted Array | ||
Iterative binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point. | ||
|
||
Tips for Implementing Iterative Binary Search | ||
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty. | ||
Prevent Overflow: When calculating the middle index, use mid = low + (high - low) / 2 instead of mid = (low + high) / 2 to prevent potential overflow. | ||
Efficiency: The iterative approach often uses less memory than the recursive approach because it doesn't involve the overhead of multiple recursive function calls. | ||
|
||
In Conclusion | ||
Iterative binary search is a fundamental algorithm that every programmer should master. Its efficiency and versatility make it a powerful tool for solving a wide range of problems. By understanding how iterative binary search works and how to implement its variations, you'll be well-equipped to tackle numerous challenges in your programming journey. Happy coding! |
This file contains hidden or 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,8 @@ | ||
{ | ||
"label": "Binary Search", | ||
"position": 7, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). " | ||
} | ||
} |
This file contains hidden or 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,135 @@ | ||
--- | ||
id: binary-search-DSA | ||
title: Binary Search | ||
sidebar_label: Binary Search | ||
sidebar_position: 6 | ||
description: "In this blog post, we'll dive into the binary search algorithm, a fundamental technique in computer science for efficiently finding an element in a sorted array. You'll learn what binary search is, how it works, and its time complexity. We'll also cover practical applications, variations of binary search, and common problems you can solve using this algorithm. By the end, you'll have a thorough understanding of binary search and how to implement it in your programming projects." | ||
tags: [dsa, algorithms, binary search] | ||
--- | ||
|
||
Welcome, eager learner! Today, we embark on an enlightening journey through the world of binary search. This powerful algorithm is essential for efficiently finding elements in sorted arrays, making it a staple in the toolkit of any adept programmer. Whether you're optimizing search operations or solving complex algorithmic challenges, understanding binary search is crucial. Let's delve into its mechanics, applications, and implementation. | ||
|
||
## What is Binary Search? | ||
|
||
Binary search is a highly efficient algorithm used to find an element in a sorted array. It works by repeatedly dividing the search interval in half. If the value of the search key is less than the item in the middle of the interval, the algorithm narrows the interval to the lower half. Otherwise, it narrows it to the upper half. The process continues until the search key is found or the interval is empty. | ||
|
||
In pseudo-code, binary search is defined as follows: | ||
|
||
```cpp | ||
FUNCTION binarySearch(array, key): | ||
low = 0 | ||
high = array.length - 1 | ||
WHILE low <= high: | ||
mid = (low + high) / 2 | ||
IF array[mid] == key: | ||
RETURN mid | ||
ELSE IF array[mid] < key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid - 1 | ||
RETURN -1 | ||
``` | ||
|
||
In C++, this can be represented as: | ||
|
||
```cpp | ||
int binarySearch(int array[], int size, int key) { | ||
int low = 0; | ||
int high = size - 1; | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
if (array[mid] == key) { | ||
return mid; | ||
} else if (array[mid] < key) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
``` | ||
|
||
How Binary Search Works | ||
Step-by-Step Explanation | ||
|
||
1. Initialize: Set two pointers, low at the beginning and high at the end of the array. | ||
2. Middle Element: Calculate the middle element's index. | ||
3. Comparison: | ||
If the middle element is the target, return its index. | ||
If the middle element is less than the target, discard the left half by setting low to mid + 1. | ||
If the middle element is greater than the target, discard the right half by setting high to mid - 1. | ||
4. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer. | ||
|
||
Time Complexity | ||
The time complexity of binary search is | ||
𝑂 | ||
( | ||
log | ||
| ||
𝑛 | ||
) | ||
O(logn), where | ||
𝑛 | ||
n is the number of elements in the array. This logarithmic time complexity makes binary search significantly faster than linear search for large datasets. | ||
|
||
Practical Applications | ||
Binary search is widely used in various real-world applications and algorithmic problems: | ||
|
||
1. Searching in a Sorted Array | ||
The primary use of binary search is to find elements in a sorted array efficiently. It is the foundation for more complex search algorithms. | ||
|
||
2. Dictionary Lookups | ||
Binary search is used in dictionaries (like the one you're reading now) to quickly find words and their definitions. | ||
|
||
3. Binary Search Trees | ||
Binary search is the basis for searching in binary search trees (BSTs), a fundamental data structure in computer science. | ||
|
||
4. Finding Boundaries | ||
Binary search can be adapted to find the first or last occurrence of a target element, making it useful in problems requiring boundary searches. | ||
|
||
Variations of Binary Search | ||
Binary search can be adapted in various ways to solve different types of problems. Here are a couple of common variations: | ||
|
||
1. Lower Bound and Upper Bound | ||
These variations of binary search are used to find the first and last occurrence of a target element in a sorted array. | ||
|
||
Lower Bound Pseudo-Code: | ||
|
||
```cpp | ||
FUNCTION lowerBound(array, key): | ||
low = 0 | ||
high = array.length | ||
WHILE low < high: | ||
mid = (low + high) / 2 | ||
IF array[mid] < key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid | ||
RETURN low | ||
``` | ||
|
||
Upper Bound Pseudo-Code: | ||
|
||
```cpp | ||
FUNCTION upperBound(array, key): | ||
low = 0 | ||
high = array.length | ||
WHILE low < high: | ||
mid = (low + high) / 2 | ||
IF array[mid] <= key: | ||
low = mid + 1 | ||
ELSE: | ||
high = mid | ||
RETURN low | ||
``` | ||
|
||
2. Rotated Sorted Array | ||
Binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point. | ||
|
||
Tips for Implementing Binary Search | ||
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty. | ||
Prevent Overflow: When calculating the middle index, use mid = low + (high - low) / 2 instead of mid = (low + high) / 2 to prevent potential overflow. | ||
Iterative vs. Recursive: Both iterative and recursive implementations are valid. Choose based on your preference and the problem constraints. | ||
In Conclusion | ||
Binary search is a fundamental algorithm that every programmer should master. Its efficiency and versatility make it a powerful tool for solving a wide range of problems. By understanding how binary search works and how to implement its variations, you'll be well-equipped to tackle numerous challenges in your programming journey. Happy coding! |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.