8000 Binary_Search_in2D_Array.cpp by chhotu2 · Pull Request #656 · dheeraj-2000/dsalgo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Binary_Search_in2D_Array.cpp #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master 8000
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Algorithms/Binary_Search_in2D Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#include <iostream>
#include <vector>
using namespace std;

bool binarySearch(vector<vector<int>> arr, int row, int col, int target)
{
int start = 0;
int end = row * col - 1;
int mid = start + (end - start) / 2;
while (start <= end)
{
int element = arr[mid / 4][mid % 4];
if (element == target)
{
return 1;
}

else if (element > target)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end - start) / 2;
}
return 0;
}
int main()
{
vector<vector<int>> arr{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int row = arr.size();
int col = arr[0].size();
int k = 10; // element to search

if (binarySearch(arr, row, col, k))
{
cout << "Element Found!" << endl;
}

else
{
cout << "Element Not Found!" << endl;
}

return 0;
}
0