Skip to content

Commit 781e9e9

Browse files
authored
Merge pull request #909 from SadafKausar2025/binarysearch
Added Binary Search Topic
2 parents c63b4c3 + 6dc95df commit 781e9e9

File tree

5 files changed

+650
-126
lines changed

5 files changed

+650
-126
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
id: iterative-binary-search-DSA
3+
title: Iterative Binary Search
4+
sidebar_label: Iterative Binary Search
5+
sidebar_position: 7
6+
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."
7+
tags: [dsa, algorithms, binary search, iterative]
8+
---
9+
10+
Iterative Binary Search is powerful algorithm that 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.
11+
12+
## What is Iterative Binary Search?
13+
14+
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.
15+
16+
In pseudo-code, iterative binary search is defined as follows:
17+
18+
```cpp
19+
FUNCTION iterativeBinarySearch(array, key):
20+
low = 0
21+
high = array.length - 1
22+
WHILE low <= high:
23+
mid = (low + high) / 2
24+
IF array[mid] == key:
25+
RETURN mid
26+
ELSE IF array[mid] < key:
27+
low = mid + 1
28+
ELSE:
29+
high = mid - 1
30+
RETURN -1
31+
```
32+
33+
```cpp
34+
int iterativeBinarySearch(int array[], int size, int key) {
35+
int low = 0;
36+
int high = size - 1;
37+
while (low <= high) {
38+
int mid = low + (high - low) / 2;
39+
if (array[mid] == key) {
40+
return mid;
41+
} else if (array[mid] < key) {
42+
low = mid + 1;
43+
} else {
44+
high = mid - 1;
45+
}
46+
}
47+
return -1;
48+
}
49+
```
50+
51+
## How Iterative Binary Search Works
52+
53+
### Step-by-Step Explanation
54+
55+
1. Initialize: Set two pointers, low at the beginning and high at the end of the array.
56+
2. Middle Element: Calculate the middle element's index.
57+
Comparison:
58+
3. If the middle element is the target, return its index.
59+
4. If the middle element is less than the target, discard the left half by setting low to mid + 1.
60+
5. If the middle element is greater than the target, discard the right half by setting high to mid - 1.
61+
6. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
62+
63+
### Time Complexity
64+
65+
The time complexity of iterative binary search is $O(logn)$,
66+
where $𝑛$ is the number of elements in the array. This logarithmic time complexity makes iterative binary search significantly faster than linear search for large datasets.
67+
68+
## Practical Applications
69+
70+
Iterative binary search is widely used in various real-world applications and algorithmic problems:
71+
72+
1. Searching in a Sorted Array
73+
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.
74+
75+
2. Dictionary Lookups
76+
Iterative binary search is used in dictionaries (like the one you're reading now) to quickly find words and their definitions.
77+
78+
3. Binary Search Trees
79+
Iterative binary search is the basis for searching in binary search trees (BSTs), a fundamental data structure in computer science.
80+
81+
4. Finding Boundaries
82+
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.
83+
84+
Common Problems Solved Using Iterative Binary Search
85+
Iterative binary search can be adapted in various ways to solve different types of problems. Here are a couple of common problems:
86+
87+
1. Lower Bound and Upper Bound
88+
These variations of iterative binary search are used to find the first and last occurrence of a target element in a sorted array.
89+
90+
Lower Bound Pseudo-Code:
91+
92+
```cpp
93+
FUNCTION lowerBound(array, key):
94+
low = 0
95+
high = array.length
96+
WHILE low < high:
97+
mid = (low + high) / 2
98+
IF array[mid] < key:
99+
low = mid + 1
100+
ELSE:
101+
high = mid
102+
RETURN low
103+
104+
```
105+
106+
Upper Bound Pseudo-Code:
107+
108+
```cpp
109+
FUNCTION upperBound(array, key):
110+
low = 0
111+
high = array.length
112+
WHILE low < high:
113+
mid = (low + high) / 2
114+
IF array[mid] <= key:
115+
low = mid + 1
116+
ELSE:
117+
high = mid
118+
RETURN low
119+
120+
121+
```
122+
123+
2. Rotated Sorted Array
124+
Iterative binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
125+
126+
:::tip
127+
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
128+
Prevent Overflow: When calculating the middle index, use $\text{mid} = \text{low} + \frac{\text{high} - \text{low}}{2}$ instead of $\text{mid} = \frac{\text{low} + \text{high}}{2}$ to prevent potential overflow.
129+
Efficiency: The iterative approach often uses less memory than the recursive approach because it doesn't involve the overhead of multiple recursive function calls.
130+
:::
131+
132+
## Conclusion
133+
134+
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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Binary Search",
3+
"position": 7,
4+
"link": {
5+
"type": "generated-index",
6+
"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). "
7+
}
8+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
id: binary-search-DSA
3+
title: Binary Search
4+
sidebar_label: Binary Search
5+
sidebar_position: 6
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."
7+
tags: [dsa, algorithms, binary search]
8+
---
9+
10+
Binary Search 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.
11+
12+
## What is Binary Search?
13+
14+
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.
15+
16+
In pseudo-code, binary search is defined as follows:
17+
18+
```cpp
19+
FUNCTION binarySearch(array, key):
20+
low = 0
21+
high = array.length - 1
22+
WHILE low <= high:
23+
mid = (low + high) / 2
24+
IF array[mid] == key:
25+
RETURN mid
26+
ELSE IF array[mid] < key:
27+
low = mid + 1
28+
ELSE:
29+
high = mid - 1
30+
RETURN -1
31+
```
32+
33+
In C++, this can be represented as:
34+
35+
```cpp
36+
int binarySearch(int array[], int size, int key) {
37+
int low = 0;
38+
int high = size - 1;
39+
while (low <= high) {
40+
int mid = low + (high - low) / 2;
41+
if (array[mid] == key) {
42+
return mid;
43+
} else if (array[mid] < key) {
44+
low = mid + 1;
45+
} else {
46+
high = mid - 1;
47+
}
48+
}
49+
return -1;
50+
}
51+
```
52+
53+
## How Binary Search Works
54+
55+
### Step-by-Step Explanation
56+
57+
1. Initialize: Set two pointers, low at the beginning and high at the end of the array.
58+
2. Middle Element: Calculate the middle element's index.
59+
3. Comparison:
60+
If the middle element is the target, return its index.
61+
If the middle element is less than the target, discard the left half by setting low to mid + 1.
62+
If the middle element is greater than the target, discard the right half by setting high to mid - 1.
63+
4. Repeat: Repeat steps 2 and 3 until the target is found or the low pointer exceeds the high pointer.
64+
65+
### Time Complexity
66+
67+
The time complexity of binary search is $𝑂(log𝑛)$
68+
69+
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.
70+
71+
## Practical Applications
72+
73+
Binary search is widely used in various real-world applications and algorithmic problems:
74+
75+
1. Searching in a Sorted Array
76+
The primary use of binary search is to find elements in a sorted array efficiently. It is the foundation for more complex search algorithms.
77+
78+
2. Dictionary Lookups
79+
Binary search is used in dictionaries (like the one you're reading now) to quickly find words and their definitions.
80+
81+
3. Binary Search Trees
82+
Binary search is the basis for searching in binary search trees (BSTs), a fundamental data structure in computer science.
83+
84+
4. Finding Boundaries
85+
Binary search can be adapted to find the first or last occurrence of a target element, making it useful in problems requiring boundary searches.
86+
87+
Variations of Binary Search
88+
Binary search can be adapted in various ways to solve different types of problems. Here are a couple of common variations:
89+
90+
1. Lower Bound and Upper Bound
91+
These variations of binary search are used to find the first and last occurrence of a target element in a sorted array.
92+
93+
Lower Bound Pseudo-Code:
94+
95+
```cpp
96+
FUNCTION lowerBound(array, key):
97+
low = 0
98+
high = array.length
99+
WHILE low < high:
100+
mid = (low + high) / 2
101+
IF array[mid] < key:
102+
low = mid + 1
103+
ELSE:
104+
high = mid
105+
RETURN low
106+
```
107+
108+
Upper Bound Pseudo-Code:
109+
110+
```cpp
111+
FUNCTION upperBound(array, key):
112+
low = 0
113+
high = array.length
114+
WHILE low < high:
115+
mid = (low + high) / 2
116+
IF array[mid] <= key:
117+
low = mid + 1
118+
ELSE:
119+
high = mid
120+
RETURN low
121+
```
122+
123+
2. Rotated Sorted Array
124+
Binary search can be modified to handle rotated sorted arrays, where the array is sorted but then rotated at some pivot point.
125+
126+
:::Tip
127+
Handle Edge Cases: Ensure your implementation correctly handles cases where the target element is not present or when the array is empty.
128+
Prevent Overflow: When calculating the middle index, use $\text{mid} = \text{low} + \frac{\text{high} - \text{low}}{2}$ instead of $\text{mid} = \frac{\text{low} + \text{high}}{2}$ to prevent potential overflow.
129+
Iterative vs. Recursive: Both iterative and recursive implementations are valid. Choose based on your preference and the problem constraints.
130+
:::
131+
132+
## Conclusion
133+
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.

0 commit comments

Comments
 (0)