Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
saddleback_search.cpp File Reference

Implementation of Saddleback Algorithm for 2D arrays. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for saddleback_search.cpp:

Namespaces

namespace  search
 for std::vector
 
namespace  saddleback
 Function for implementing Saddleback Algorithm.
 

Functions

std::pair< uint32_t, uint32_t > search::saddleback::saddleback (std::vector< std::vector< int32_t > > matrix, int32_t element)
 
static void test ()
 Test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of Saddleback Algorithm for 2D arrays.

Saddleback Algorithm is an algorithm that searches 2D array in linear time, i.e, O(m + n), where m is number of rows and n is number of columns of 2D array. Also, each row and column of the matrix should be sorted beforehand for this algorithm to work.

Author
Hashir Niazi

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
103 {
104 test(); // execute the tests
105 return 0;
106}
static void test()
Test implementations.
Definition saddleback_search.cpp:70
Here is the call graph for this function:

◆ saddleback()

std::pair< uint32_t, uint32_t > search::saddleback::saddleback ( std::vector< std::vector< int32_t > > matrix,
int32_t element )

This function implements Saddleback Algorithm, on a sorted 2D array, and finds the location of the element needed to search

Parameters
matrix2D matrix which is sorted on the basis of rows and columns
elementelement to be searched
Returns
An std::pair of with row and column populated within it, if the element is present.
An std::pair with (0, 0), if the element is not present.
34 {
35 uint32_t left_index = 0;
36 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.
38 if (element ==
39 matrix[left_index]
40 [right_index]) { // If value on this position of matrix is
41 // equal to element, return (row, column).
42 return std::make_pair(left_index+1, right_index+1);
43 } else if (element >
44 matrix[left_index]
45 [right_index]) { // Else if value on this position of
46 // matrix is less than the element,
47 // move left.
48 ++left_index;
49 } else if (element <
50 matrix[left_index]
51 [right_index]) { // Else if value on this position of
52 // matrix is greater than the
53 // element, move down.
54 if(!right_index)
55 break;
56 else --right_index;
57 }
58 }
59 return std::make_pair(
60 0, 0); // If the program reaches here, that means one of the index
61 // went out of index, hence no element present.
62}
T make_pair(T... args)
T size(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Test implementations.

Returns
void
70 {
71 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}};
76
79 // Test 1
81 assert(not_found == answer1);
82 // Test 2
84 assert(not_found == answer1);
85 // Test 3
87 test_answer = std::make_pair(1, 1);
88 assert(test_answer == answer1);
89 // Test 4
90 answer1 = search::saddleback::saddleback(matrix, 50000);
91 test_answer = std::make_pair(5, 5);
92 assert(test_answer == answer1);
93 // Test 5
95 test_answer = std::make_pair(3, 3);
96 assert(test_answer == answer1);
97}
std::pair< uint32_t, uint32_t > saddleback(std::vector< std::vector< int32_t > > matrix, int32_t element)
Definition saddleback_search.cpp:33
Here is the call graph for this function: