Skip to content

Commit 52ff5e8

Browse files
fix: add <cstdint> to search/**
1 parent 6357548 commit 52ff5e8

File tree

6 files changed

+36
-23
lines changed

6 files changed

+36
-23
lines changed

search/binary_search.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include <algorithm> /// for std::sort function
3838
#include <cassert> /// for std::assert
39+
#include <cstdint> /// for integral typedefs
3940
#include <iostream> /// for IO operations
4041
#include <vector> /// for std::vector
4142
/******************************************************************************

search/exponential_search.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
#include <cassert>
1616
#include <cmath>
17-
#include <iostream>
17+
#include <cstdint>
18+
#include <cstdint> /// for integral typedefs
1819
#ifdef _MSC_VER
1920
#include <string> // use for MS Visual C++
2021
#else

search/floyd_cycle_detection_algo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313

1414
#include <cassert> /// for assert
15+
#include <cstdint> /// for integral typedefs
1516
#include <iostream> /// for IO operations
1617
#include <vector> /// for std::vector
17-
1818
/**
1919
* @namespace search
2020
* @brief Search algorithms

search/interpolation_search.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <algorithm> /// for std::sort function
3333
#include <cassert> /// for std::assert
34+
#include <cstdint> /// for integral typedefs
3435
#include <iostream> /// for IO operations
3536
#include <vector> /// for std::vector
3637

search/saddleback_search.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
11
/**
22
* @file
3-
* @brief Implementation of [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array) for 2D arrays.
3+
* @brief Implementation of [Saddleback
4+
* Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array)
5+
* for 2D arrays.
46
*
57
* @details
68
* Saddleback Algorithm is an algorithm that searches 2D array in linear time,
7-
* i.e, O(m + n), where m is number of rows and n is number of columns of 2D array. Also, each row and
8-
* column of the matrix should be sorted beforehand for this algorithm to work.
9+
* i.e, O(m + n), where m is number of rows and n is number of columns of 2D
10+
* array. Also, each row and column of the matrix should be sorted beforehand
11+
* for this algorithm to work.
912
*
1013
* @author [Hashir Niazi](https://github.com/HashirGJ8842)
1114
*/
12-
#include <cassert> /// for assert
13-
#include <iostream> /// for io operations, and std::pair
14-
#include <vector> /// for std::vector
15+
#include <cassert> /// for assert
16+
#include <cstdint> /// for integral typedefs
17+
#include <iostream> /// for io operations, and std::pair
18+
#include <vector> /// for std::vector
1519

1620
/** \namespace search
1721
* \brief Algorithms for searching
1822
*/
1923
namespace search {
2024
/** \namespace saddleback
21-
* \brief Function for implementing [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array).
25+
* \brief Function for implementing [Saddleback
26+
* Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array).
2227
*/
2328
namespace saddleback {
2429
/**
25-
* This function implements [Saddleback Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array),
30+
* This function implements [Saddleback
31+
* Algorithm](https://www.geeksforgeeks.org/saddleback-search-algorithm-in-a-2d-array),
2632
* on a sorted 2D array, and finds the location of the element needed to search
2733
* @param matrix 2D matrix which is sorted on the basis of rows and columns
2834
* @param element element to be searched
2935
* @return An std::pair of with row and column populated within it, if the
3036
* element is present.
3137
* @return An std::pair with (0, 0), if the element is not present.
3238
*/
33-
std::pair<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matrix,
34-
int32_t element) {
39+
std::pair<uint32_t, uint32_t> saddleback(
40+
std::vector<std::vector<int32_t>> matrix, int32_t element) {
3541
uint32_t left_index = 0;
3642
uint32_t right_index = matrix[0].size() - 1; // Start from top right corner
37-
while (left_index < matrix.size()) { // Exit once the value of indexes get out of range.
43+
while (left_index <
44+
matrix.size()) { // Exit once the value of indexes get out of range.
3845
if (element ==
3946
matrix[left_index]
4047
[right_index]) { // If value on this position of matrix is
4148
// equal to element, return (row, column).
42-
return std::make_pair(left_index+1, right_index+1);
49+
return std::make_pair(left_index + 1, right_index + 1);
4350
} else if (element >
4451
matrix[left_index]
4552
[right_index]) { // Else if value on this position of
@@ -51,14 +58,15 @@ std::pair<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matri
5158
[right_index]) { // Else if value on this position of
5259
// matrix is greater than the
5360
// element, move down.
54-
if(!right_index)
61+
if (!right_index)
5562
break;
56-
else --right_index;
63+
else
64+
--right_index;
5765
}
5866
}
5967
return std::make_pair(
6068
0, 0); // If the program reaches here, that means one of the index
61-
// went out of index, hence no element present.
69+
// went out of index, hence no element present.
6270
}
6371
} // namespace saddleback
6472
} // namespace search
@@ -69,15 +77,16 @@ std::pair<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matri
6977
*/
7078
static void test() {
7179
std::vector<std::vector<int32_t>> matrix = {{1, 10, 100, 1000, 10000},
72-
{2, 20, 200, 2000, 20000},
73-
{3, 30, 300, 3000, 30000},
74-
{4, 40, 400, 4000, 40000},
75-
{5, 50, 500, 5000, 50000}};
80+
{2, 20, 200, 2000, 20000},
81+
{3, 30, 300, 3000, 30000},
82+
{4, 40, 400, 4000, 40000},
83+
{5, 50, 500, 5000, 50000}};
7684

7785
std::pair<uint32_t, uint32_t> not_found = std::make_pair(0, 0);
7886
std::pair<uint32_t, uint32_t> test_answer;
7987
// Test 1
80-
std::pair<uint32_t, uint32_t> answer1 = search::saddleback::saddleback(matrix, 123);
88+
std::pair<uint32_t, uint32_t> answer1 =
89+
search::saddleback::saddleback(matrix, 123);
8190
assert(not_found == answer1);
8291
// Test 2
8392
answer1 = search::saddleback::saddleback(matrix, 0);
@@ -101,6 +110,6 @@ static void test() {
101110
* @returns 0 on exit
102111
*/
103112
int main() {
104-
test(); // execute the tests
113+
test(); // execute the tests
105114
return 0;
106115
}

search/sublist_search.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include <cassert> /// for assert
29+
#include <cstdint> /// for integral typedefs
2930
#include <iostream> /// for IO operations
3031
#include <vector> /// for std::vector
3132

0 commit comments

Comments
 (0)