1
1
/* *
2
2
* @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.
4
6
*
5
7
* @details
6
8
* 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.
9
12
*
10
13
* @author [Hashir Niazi](https://github.com/HashirGJ8842)
11
14
*/
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
15
19
16
20
/* * \namespace search
17
21
* \brief Algorithms for searching
18
22
*/
19
23
namespace search {
20
24
/* * \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).
22
27
*/
23
28
namespace saddleback {
24
29
/* *
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),
26
32
* on a sorted 2D array, and finds the location of the element needed to search
27
33
* @param matrix 2D matrix which is sorted on the basis of rows and columns
28
34
* @param element element to be searched
29
35
* @return An std::pair of with row and column populated within it, if the
30
36
* element is present.
31
37
* @return An std::pair with (0, 0), if the element is not present.
32
38
*/
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) {
35
41
uint32_t left_index = 0 ;
36
42
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.
38
45
if (element ==
39
46
matrix[left_index]
40
47
[right_index]) { // If value on this position of matrix is
41
48
// 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 );
43
50
} else if (element >
44
51
matrix[left_index]
45
52
[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
51
58
[right_index]) { // Else if value on this position of
52
59
// matrix is greater than the
53
60
// element, move down.
54
- if (!right_index)
61
+ if (!right_index)
55
62
break ;
56
- else --right_index;
63
+ else
64
+ --right_index;
57
65
}
58
66
}
59
67
return std::make_pair (
60
68
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.
62
70
}
63
71
} // namespace saddleback
64
72
} // namespace search
@@ -69,15 +77,16 @@ std::pair<uint32_t, uint32_t> saddleback(std::vector<std::vector<int32_t>> matri
69
77
*/
70
78
static void test () {
71
79
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 }};
76
84
77
85
std::pair<uint32_t , uint32_t > not_found = std::make_pair (0 , 0 );
78
86
std::pair<uint32_t , uint32_t > test_answer;
79
87
// 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 );
81
90
assert (not_found == answer1);
82
91
// Test 2
83
92
answer1 = search::saddleback::saddleback (matrix, 0 );
@@ -101,6 +110,6 @@ static void test() {
101
110
* @returns 0 on exit
102
111
*/
103
112
int main () {
104
- test (); // execute the tests
113
+ test (); // execute the tests
105
114
return 0 ;
106
115
}
0 commit comments