Algorithm
Algorithm - Comprehensive C++ Programming Guide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* ==================================================================================
* █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
* █ Algorithm - Comprehensive C++ Programming Guide █
* █▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█
*
* @description A comprehensive guide to C++ Algorithm implementation and best practices.
* Part of the C++ Programming Header Series.
*
* @author Shahrear Hossain Shawon
* @github algoscienceacademy
* @institution International Islamic University Chittagong (IIUC)
*
* @version 1.0.0
* @date Created: January 25, 2025
* Updated: January 27, 2025
*
* @credits C++ Standard Library
* C++ Reference
* ChatGPT
*
* @license MIT License
*
* @copyright Copyright (c) 2025
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ==================================================================================
*/
CPP UTF-8
Lines: 44
Introduction of Algorithm
1
2
3
4
The <algorithm> header in C++ is part of the Standard Template Library (STL)
and provides a rich collection of functions to perform common operations on
containers like std::vector, std::list, std::deque, and others. It focuses
on non-modifying and modifying sequence operations, sorting, searching, and more.
CPP UTF-8
Lines: 4
Algorithm Features
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
------------------------------------------------------------------------------------------------------------------------------------------
| . Modular and Reusable Code | Contains generic algorithms that can operate on a variety of container types. |
| | Uses iterators to provide flexibility in working with different container types. |
..........................................|..............................................................................................|
| . Non-Modifying Sequence Operations | Functions that do not alter the input data, such as std::find, std::count, std::all_of, etc. |
..........................................|..............................................................................................|
| . Modifying Sequence Operations | Functions that modify the container or range, like std::copy, std::remove, std::replace. |
..........................................|..............................................................................................|
| . Sorting and Partitioning | Includes efficient sorting algorithms like std::sort, std::stable_sort, and partitioning |
| | algorithms like std::partition. |
..........................................|..............................................................................................|
| . Searching and Merging | Provides functions like std::binary_search, std::merge, and std::lower_bound. |
..........................................|..............................................................................................|
| . Heap Operations | Functions like std::make_heap, std::push_heap, std::pop_heap for working with heaps. |
..........................................|..............................................................................................|
| . Set Operations | Functions like std::set_union, std::set_difference to operate on sorted ranges as |
| | mathematical sets. |
..........................................|..............................................................................................|
| . Randomized Algorithms | ncludes functions like std::shuffle and std::sample for randomizing ranges. |
..........................................|..............................................................................................|
| . Utility Functions | Helper functions like std::min, std::max, std::clamp, and std::lexicographical_compare. |
------------------------------------------------------------------------------------------------------------------------------------------
CPP UTF-8
Lines: 22
General Categories of Algorithms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---Non-Modifying Sequence Operations: ---| Operations that do not change the original range of elements.
| Examples: std::find, std::count, std::mismatch.
..........................................................................................................
---Modifying Sequence Operations: -------| Operations that modify the original range of elements.
| Examples: std::copy, std::remove, std::replace.
..........................................................................................................
---Sorting and Partitioning: ------------| Operations that sort or partition the elements in a range.
| Examples: std::sort, std::stable_sort, std::partition.
..........................................................................................................
---Binary Search: -----------------------| Operations that search for an element in a sorted range.
| Examples: std::binary_search, std::lower_bound, std::upper_bound.
..........................................................................................................
---Heap Operations: ---------------------| Operations that work with heap data structures.
| Examples: std::make_heap, std::push_heap, std::pop_heap.
..........................................................................................................
---Set Operations: ----------------------| Operations that work with sorted ranges as mathematical sets.
| Examples: std::set_union, std::set_difference.
..........................................................................................................
---Randomized Algorithms: ---------------| Operations that involve randomness.
| Examples: std::shuffle, std::sample.
..........................................................................................................
---Numeric Operations: ------------------| Operations that work with numeric values.
| Examples: std::accumulate, std::inner_product.
..........................................................................................................
CPP UTF-8
Lines: 24
Algorithm Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
---Non-Modifying Sequence Operations: ---| all_of, any_of, none_of, for_each, count, count_if, mismatch, equal, find, find_if, find_if_not, find_end, find_first_of, adjacent_find, search, search_n.
..........................................................................................................
---Modifying Sequence Operations: -------| copy, copy_n, copy_if, copy_backward, move, move_backward, fill, fill_n, transform, generate, generate_n, remove, remove_if, remove_copy, remove_copy_if, replace, replace_if, replace_copy, replace_copy_if, swap, swap_ranges, iter_swap, reverse, reverse_copy, rotate, rotate_copy, random_shuffle, shuffle.
..........................................................................................................
---Sorting and Partitioning: ------------| is_sorted, is_sorted_until, sort, partial_sort, partial_sort_copy, stable_sort, nth_element, partition, partition_point, is_partitioned, inplace_merge.
..........................................................................................................
---Binary Search: -----------------------| lower_bound, upper_bound, binary_search, equal_range.
..........................................................................................................
---Heap Operations: ---------------------| is_heap, is_heap_until, make_heap, push_heap, pop_heap, sort_heap.
..........................................................................................................
---Set Operations: ----------------------| merge, includes, set_union, set_intersection, set_difference, set_symmetric_difference.
..........................................................................................................
---Randomized Algorithms: ---------------| random_shuffle, shuffle, sample.
..........................................................................................................
CPP UTF-8
Lines: 14
Example 1-23
std::max_element and std::min_element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {10, 20, 5, 15, 30};
// Find the maximum element
auto max_it = std::max_element(nums.begin(), nums.end());
std::cout << "Maximum element: " << *max_it << "
";
// Find the minimum element
auto min_it = std::min_element(nums.begin(), nums.end());
std::cout << "Minimum element: " << *min_it << "
";
return 0;
}
CPP UTF-8
Lines: 19
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Maximum element: 30
Minimum element: 5
CPP UTF-8
Lines: 3
std::sort with Custom Comparator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums {10, 20, 5, 15, 30};
// Sort in descending order
std::sort(nums.begin(), nums.end(), [](int a, int b) {
return a > b; // Custom comparator
});
// Print sorted elements
std::cout << "Sorted in descending order: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 22
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Sorted in descending order: 30 20 15 10 5
CPP UTF-8
Lines: 3
std::unique to Remove Consecutive Duplicates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 1, 2, 2, 3, 4, 4, 5};
// Remove consecutive duplicates
auto it = std::unique(nums.begin(), nums.end());
// Resize vector to the new size
nums.erase(it, nums.end());
// Print the unique elements
std::cout << "After removing duplicates: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 23
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: After removing duplicates: 1 2 3 4 5
CPP UTF-8
Lines: 2
std::partition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5, 6};
// Partition even and odd numbers
auto partition_point = std::partition(nums.begin(), nums.end(), [](int n) {
return n % 2 == 0; // Even numbers first
});
// Sort the even numbers
std::sort(nums.begin(), partition_point);
// Sort the odd numbers
std::sort(partition_point, nums.end());
// Print partitioned elements
std::cout << "Partitioned (evens first): ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 29
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Partitioned (evens first): 2 4 6 1 3 5
CPP UTF-8
Lines: 2
std::transform
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
std::vector<int> squared(nums.size());
// Square each element
std::transform(nums.begin(), nums.end(), squared.begin(), [](int n) {
return n * n;
});
// Print squared elements
std::cout << "Squared elements: ";
for (int n : squared) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 23
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Squared elements: 1 4 9 16 25
CPP UTF-8
Lines: 2
std::merg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec1 = {1, 3, 5, 7};
std::vector<int> vec2 = {2, 4, 6, 8};
std::vector<int> merged(vec1.size() + vec2.size());
// Merge two sorted ranges
std::merge(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), merged.begin());
// Print merged result
std::cout << "Merged: ";
for (int n : merged) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 22
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Merged: 1 2 3 4 5 6 7 8
CPP UTF-8
Lines: 3
std::binary_search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 3, 5, 7, 9};
// Check if element exists
bool found = std::binary_search(nums.begin(), nums.end(), 5);
std::cout << "Element 5 found: " << (found ? "Yes" : "No") << "
";
found = std::binary_search(nums.begin(), nums.end(), 4);
std::cout << "Element 4 found: " << (found ? "Yes" : "No") << "
";
return 0;
}
CPP UTF-8
Lines: 18
1
2
3
4
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Element 5 found: Yes
Element 4 found: No
CPP UTF-8
Lines: 4
std::remove_if
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5, 6};
// Remove all even numbers
auto it = std::remove_if(nums.begin(), nums.end(), [](int n) {
return n % 2 == 0; // Remove if even
});
// Erase the removed elements
nums.erase(it, nums.end());
// Print the result
std::cout << "After removing evens: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 25
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: After removing evens: 1 3 5
CPP UTF-8
Lines: 3
std::rotate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
// Rotate elements left
std::rotate(nums.begin(), nums.begin() + 2, nums.end());
// Print the result
std::cout << "After rotation: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 20
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: After rotation: 3 4 5 1 2
CPP UTF-8
Lines: 2
std::nth_element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2};
// Partially sort to place the 3rd smallest element in position
std::nth_element(nums.begin(), nums.begin() + 2, nums.end());
// Print the result
std::cout << "After nth_element (3rd smallest): ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 21
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: After nth_element (3rd smallest): 1 1 2 3 4 5 9
CPP UTF-8
Lines: 2
std::set_union
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> set1 = {1, 3, 5, 7};
std::vector<int> set2 = {3, 5, 8, 9};
std::vector<int> result(set1.size() + set2.size());
// Perform set union
auto it = std::set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin());
result.resize(it - result.begin());
// Print union result
std::cout << "Union: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 23
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Union: 1 3 5 7 8 9
CPP UTF-8
Lines: 3
std::find_if
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5, 6};
// Find the first odd number
auto it = std::find_if(nums.begin(), nums.end(), [](int n) {
return n % 2 != 0; // Condition: odd
});
if (it != nums.end()) {
std::cout << "First odd number: " << *it << "
";
} else {
std::cout << "No odd number found.
";
}
return 0;
}
CPP UTF-8
Lines: 22
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: First odd number: 1
CPP UTF-8
Lines: 2
std::reverse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
// Reverse the vector
std::reverse(nums.begin(), nums.end());
// Print reversed vector
std::cout << "Reversed: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 21
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Reversed: 5 4 3 2 1
CPP UTF-8
Lines: 2
std::accumulate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <vector>
#include <numeric> // For std::accumulate
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 4, 5};
// Calculate the sum of all elements
int sum = std::accumulate(nums.begin(), nums.end(), 0);
std::cout << "Sum of elements: " << sum << "
";
return 0;
}
CPP UTF-8
Lines: 16
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Sum of elements: 15
CPP UTF-8
Lines: 2
std::equal_range
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3, 3, 3, 4, 5};
// Find the range of value 3
auto range = std::equal_range(nums.begin(), nums.end(), 3);
std::cout << "Range of value 3: ";
for (auto it = range.first; it != range.second; ++it) {
std::cout << *it << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 19
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Range of value 3: 3 3 3
CPP UTF-8
Lines: 2
std::inplace_merge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 3, 5, 2, 4, 6};
// Merge the two sorted halves in-place
std::inplace_merge(nums.begin(), nums.begin() + 3, nums.end());
// Print the result
std::cout << "After in-place merge: ";
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 21
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: After in-place merge: 1 2 3 4 5 6
CPP UTF-8
Lines: 2
std::minmax_element
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5, 9};
// Find the min and max elements
auto result = std::minmax_element(nums.begin(), nums.end());
std::cout << "Min element: " << *result.first << "
";
std::cout << "Max element: " << *result.second << "
";
return 0;
}
CPP UTF-8
Lines: 18
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Min element: 1
Max element: 9
CPP UTF-8
Lines: 3
std::next_permutation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {1, 2, 3};
// Generate and print all permutations
do {
for (int n : nums) {
std::cout << n << " ";
}
std::cout << "
";
} while (std::next_permutation(nums.begin(), nums.end()));
return 0;
}
CPP UTF-8
Lines: 18
1
2
3
4
5
6
7
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: 1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
CPP UTF-8
Lines: 7
std::adjacent_difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> nums = {10, 20, 30, 40, 50};
std::vector<int> differences(nums.size());
// Calculate adjacent differences
std::adjacent_difference(nums.begin(), nums.end(), differences.begin());
// Print the differences
std::cout << "Differences: ";
for (int d : differences) {
std::cout << d << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 21
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Differences: 10 10 10 10 10
CPP UTF-8
Lines: 2
std::includes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> set1 = {1, 2, 3, 4, 5};
std::vector<int> set2 = {2, 3, 5};
// Check if set1 includes set2
bool result = std::includes(set1.begin(), set1.end(), set2.begin(), set2.end());
std::cout << "Does set1 include set2? " << (result ? "Yes" : "No") << "
";
return 0;
}
CPP UTF-8
Lines: 16
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Does set1 include set2? Yes
CPP UTF-8
Lines: 2
std::set_intersection
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> set1 = {1, 2, 3, 4, 5};
std::vector<int> set2 = {3, 4, 5, 6, 7};
std::vector<int> result(std::min(set1.size(), set2.size()));
// Find the intersection
auto it = std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin());
// Resize the result vector to the actual size
result.resize(it - result.begin());
// Print the intersection
std::cout << "Intersection: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 25
1
2
3
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Intersection: 3 4 5
CPP UTF-8
Lines: 3
std::set_difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <algorithm>
#include <vector>
#include <iostream>
int main() {
std::vector<int> set1 = {1, 2, 3, 4, 5};
std::vector<int> set2 = {3, 4, 5, 6, 7};
std::vector<int> result(std::max(set1.size(), set2.size()));
// Find the difference
auto it = std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), result.begin());
// Resize the result vector to the actual size
result.resize(it - result.begin());
// Print the difference
std::cout << "Difference: ";
for (int n : result) {
std::cout << n << " ";
}
std::cout << "
";
return 0;
}
CPP UTF-8
Lines: 27
1
2
run command : g++ -std=c++11 algorithm.cpp -o algorithm
Output: Difference: 1 2
CPP UTF-8
Lines: 2