-
Notifications
You must be signed in to change notification settings - Fork 0
/
findElementInMatrixBinarySearch.cpp
executable file
·133 lines (103 loc) · 4.05 KB
/
findElementInMatrixBinarySearch.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
struct Coordinate;
template<typename T>
Coordinate findElement(std::vector<std::vector<T> > &matrix, Coordinate origin, Coordinate dest, T elem);
template<typename T>
Coordinate findElement(std::vector<std::vector<T> > &matrix, T elem);
template<typename T>
Coordinate partitionAndSearch(std::vector<std::vector<T> > &matrix, Coordinate origin, Coordinate dest, Coordinate pivot, T elem);
struct Coordinate{
int row;
int column;
Coordinate(int r, int c): row(r), column(c){}
Coordinate(Coordinate &source): row(source.row), column(source.column){}
Coordinate(Coordinate &&source): row(std::move(source.row)), column(std::move(source.column)){}
template<typename T>
bool inbounds(std::vector<std::vector<T> > &matrix){
return row >=0 && column >= 0 &&
row < matrix.size() && column < matrix[0].size();
}
bool isBefore(Coordinate p){
return row <= p.row && column <= p.column;
}
void setToAverage(Coordinate min, Coordinate max){
row = (min.row + max.row)/2;
column = (min.column + max.column)/2;
}
bool operator==(Coordinate &other){
return this->row == other.row && this->column == other.column;
}
bool operator==(Coordinate &&other){
return this->row == other.row && this->column == other.column;
}
bool operator!=(Coordinate &&other){
return !(this->operator==(std::move(other)) );
}
bool operator!=(Coordinate &other){
return !(this->operator==(other) );
}
};
//template<typename T>
//Coordinate::inbounds(std::vector<std::vector<T> > &matrix) -> Coordinate::inbounds<T>(std::vector<std::vector<T> > &matrix);
template<typename T>
Coordinate findElement(std::vector<std::vector<T> > &matrix, Coordinate origin, Coordinate dest, T elem){
if (!origin.inbounds<T>(matrix) || !dest.inbounds<T>(matrix))
return Coordinate{-1,-1};
if (matrix[origin.row][origin.column] == elem)
return origin;
else if (!origin.isBefore(dest))
return Coordinate{-1,-1};
// Set start to start of diagonal and end to the end of the diagonal. Since the grid may not be square,
// the end of the diagonal may not equal dest
Coordinate start{origin};
int diagDist { std::min(dest.row - origin.row, dest.column - origin.column) };
Coordinate end {start.row + diagDist, start.column + diagDist};
Coordinate p {0,0};
//Do binary search on the diagonal, looking for the first element greater than elem
while (start.isBefore(end)){
p.setToAverage(start, end);
if (elem > matrix[p.row][p.column]){
start.row = p.row + 1;
start.column = p.column + 1;
}
else{
end.row = p.row - 1;
end.column = p.column -1;
}
}
// Split the grid into quadrants. Search the bottom left and the top right
return partitionAndSearch<T>(matrix, origin, dest, start, elem);
}
template<typename T>
Coordinate partitionAndSearch(std::vector<std::vector<T> > &matrix, Coordinate origin, Coordinate dest, Coordinate pivot, T elem){
Coordinate lowerLeftOrigin{pivot.row, origin.column};
Coordinate lowerLeftDest{dest.row, pivot.column -1};
Coordinate upperRightOrigin{origin.row, pivot.column};
Coordinate upperRightDest{pivot.row -1, dest.column};
Coordinate lowerLeft{findElement(matrix, lowerLeftOrigin, lowerLeftDest, elem) };
if (lowerLeft == Coordinate{-1,-1}){
return findElement<T>(matrix, upperRightOrigin, upperRightDest, elem);
}
return lowerLeft;
}
template<typename T>
Coordinate findElement(std::vector<std::vector<T> > &matrix, T elem){
Coordinate origin{0,0};
Coordinate dest{matrix.size()-1, matrix[0].size()-1};
return findElement<T>(matrix, origin, dest, elem);
}
int main(){
std::vector<std::vector<int> > matrix{{15, 20, 40, 85},
{20, 35, 80, 95},
{30, 55, 95, 105},
{40, 80, 100, 120}};
int valToFind{0};
std::cout << "Value to find: ";
std::cin >> valToFind;
auto coordElem{findElement<int>(matrix, 55)};
std::cout << (( coordElem !=Coordinate{-1,-1})? "Found element!\n": "Did not find element!\n");
return 0;
}