TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
saddleback_search.cpp File Reference

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

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

Go to the source code of this file.

Namespaces

namespace  search
 for std::assert
 
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

Definition in file saddleback_search.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 112 of file saddleback_search.cpp.

112 {
113 test(); // execute the tests
114 return 0;
115}
static void test()
Test implementations.

◆ 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.

Definition at line 39 of file saddleback_search.cpp.

40 {
41 uint32_t left_index = 0;
42 uint32_t right_index = matrix[0].size() - 1; // Start from top right corner
43 while (left_index <
44 matrix.size()) { // Exit once the value of indexes get out of range.
45 if (element ==
46 matrix[left_index]
47 [right_index]) { // If value on this position of matrix is
48 // equal to element, return (row, column).
49 return std::make_pair(left_index + 1, right_index + 1);
50 } else if (element >
51 matrix[left_index]
52 [right_index]) { // Else if value on this position of
53 // matrix is less than the element,
54 // move left.
55 ++left_index;
56 } else if (element <
57 matrix[left_index]
58 [right_index]) { // Else if value on this position of
59 // matrix is greater than the
60 // element, move down.
61 if (!right_index)
62 break;
63 else
64 --right_index;
65 }
66 }
67 return std::make_pair(
68 0, 0); // If the program reaches here, that means one of the index
69 // went out of index, hence no element present.
70}
std::vector< std::valarray< T > > matrix

◆ test()

static void test ( )
static

Test implementations.

Returns
void

Definition at line 78 of file saddleback_search.cpp.

78 {
79 std::vector<std::vector<int32_t>> matrix = {{1, 10, 100, 1000, 10000},
80 {2, 20, 200, 2000, 20000},
81 {3, 30, 300, 3000, 30000},
82 {4, 40, 400, 4000, 40000},
83 {5, 50, 500, 5000, 50000}};
84
85 std::pair<uint32_t, uint32_t> not_found = std::make_pair(0, 0);
86 std::pair<uint32_t, uint32_t> test_answer;
87 // Test 1
88 std::pair<uint32_t, uint32_t> answer1 =
90 assert(not_found == answer1);
91 // Test 2
93 assert(not_found == answer1);
94 // Test 3
96 test_answer = std::make_pair(1, 1);
97 assert(test_answer == answer1);
98 // Test 4
99 answer1 = search::saddleback::saddleback(matrix, 50000);
100 test_answer = std::make_pair(5, 5);
101 assert(test_answer == answer1);
102 // Test 5
104 test_answer = std::make_pair(3, 3);
105 assert(test_answer == answer1);
106}
std::pair< uint32_t, uint32_t > saddleback(std::vector< std::vector< int32_t > > matrix, int32_t element)