473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Binary search

How can I use binary search into 2D arrays?. I want to search a number row by row
May 18 '10 #1

✓ answered by donbock

Please enclose your source code in CODE tags. (Select the code, then press the "#" button at the top of the edit box.)

Don't let the 2D array confuse you. Arrays Revealed will demystify them.

Expand|Select|Wrap|Line Numbers
  1. int array[4][5];
This is an array of four 1D arrays, each of which has 5 elements. Each of these four 1D arrays can be sent to your BinarySearch function. That is, you need to call BinarySearch four times. The code you posted only calls it once.

13 4586
donbock
2,426 Expert 2GB
Binary search only works if the list is presorted. How is the 2D array ordered?
May 18 '10 #2
Hi donbock.
The 2D array is sorted and I want to search a number by row, besides I want to continue the search even if the number is found maybe there is another same number and when the all matrix is searched I want to display the rows where that number appear.
I will appreciate any advice. C++ if there any code, thanks.
May 18 '10 #3
Hi donbock.
I forgot to tell you that the rows into the 2D array are ordered ascending.
May 18 '10 #4
donbock
2,426 Expert 2GB
The 2D array is sorted and I want to search a number by row
Is each row sorted individually or is the entire 2D array sorted? That is, is it like this:
Expand|Select|Wrap|Line Numbers
  1. row 1:  AAACDK...
  2. row 2:  BCDJM...
  3. ...
or like this:
Expand|Select|Wrap|Line Numbers
  1. row 1:  AAAABBCDE...
  2. row 2:  GHKM...
  3. row 3:  PRU...
  4. ...
If the rows are sorted individually (first example), then you need to repeat the binary search on each row. The binary search function only operates on a 1D array.

If the entire 2D array is sorted (second example), then a single pass through the binary search function is sufficient, but the binary search function must explicitly handle the 2D array.

What is your policy for upper and lowercase characters? Is 'A' the same as 'a'? If not, which is greater? Are you sure the array sort order is consistent with this policy?

Please recap the basic algorithm for binary search.
May 18 '10 #5
@donbock
Hi donbock
Thanks for reply my post.
My 2D array is your first example(the rows are sorted individually) so like you said I have to repeat the binary search on each row and that is my real problem
how to do it into 2D array.I have more than a week trying to figure it out but I can't do it. Can you give me an example in C++ please, thanks.
May 18 '10 #6
jkmyoung
2,057 Expert 2GB
Ignore the fact that it is 2-d for the moment. Try doing your search on a single array (eg one row of the 2d array). Are you getting the results correctly for the first row? If not what results are you getting?

Clarifying question:
Are there any duplicated values within a single row of the array?
May 18 '10 #7
@jkmyoung
Hi jkmyoung
I know how to do a search on a 1D array using binary search but I do not know how to do it into a 2D array . I have C++' books, I search the internet and I can not find a single example about this matter, all the examples I had found are the binary search in 1D array .
The expert donbock told me that is possible to do the binary search into 2D array, in my program the rows are sorted in ascending and each row are individually, he told me I have to repeat the binary search on each row but I just do not know how to do it.
About your question: Yes there are duplicated values within a single row of the array but I think that is not important because what I want the program do it is to search an integer number and display on the console the row in which that number belong.
Could you give me an example of a function doing a binary search into 2D array in C++. Thanks for reply my post.
May 18 '10 #8
jkmyoung
2,057 Expert 2GB
pseudo code
Expand|Select|Wrap|Line Numbers
  1. int[depth][width] array 
  2. int searchTerm
  3.  
  4. for(i = 0; i < depth; i++)
  5.   if BinarySearch(searchTerm, array[i], 0, width - 1) returns found, print "row" +  i
  6.  
This searches the first row, then the second row, etc...
May 18 '10 #9
@jkmyoung
Hi jkmyoung
Can you help me to apply your pseudo code in this program because honestly I just got about two months studying by myself C++ . THANKS
#include <iostream>
#include <iomanip>

using namespace std;

const int depth = 4;
const int width = 5;

int BinarySearch(int [], int, int);

int main()
{
cout<<endl;
int array[depth][width] = {{5,10,22,32,45},{20,23,34,40,56},
{12,14,16,34,45},{2,6,34,45,47}};
int num;// location;

for(int i=0; i<depth; i++)
{
for(int j=0; j<width; j++)
{
cout<<setw(5)<<array[i][j];
}
cout<<endl;
}
cout << "Enter the number you are searching for: ";
cin >> num;



location= BinarySearch(array, width, num);

if (location > -1)
cout << "The number was found at index location "
<< location << endl;
else
cout << "The number was not found in the list\n";

return 0;
}

int BinarySearch(int list[], int size, int key)
{
int left, right, midpt;

left = 0;
right = size - 1;

while (left <= right)
{
midpt = (int) ((left + right) / 2);
if (key == list[midpt])
{
return midpt;
}
else if (key > list[midpt])
left = midpt + 1;
else
right = midpt - 1;
}

return -1;
}

// pseudo code

// int[depth][width] array
// int searchTerm

// for(i = 0; i < depth; i++)
// if BinarySearch(searchTerm, array[i], 0, width - 1) returns found, print "row" + i
May 18 '10 #10
donbock
2,426 Expert 2GB
Please enclose your source code in CODE tags. (Select the code, then press the "#" button at the top of the edit box.)

Don't let the 2D array confuse you. Arrays Revealed will demystify them.

Expand|Select|Wrap|Line Numbers
  1. int array[4][5];
This is an array of four 1D arrays, each of which has 5 elements. Each of these four 1D arrays can be sent to your BinarySearch function. That is, you need to call BinarySearch four times. The code you posted only calls it once.
May 19 '10 #11
jkmyoung
2,057 Expert 2GB
Eg:
location= BinarySearch(array[0], width, num);
calls it on the first row of the 2d array. Call Binary search on all of the rows.
May 19 '10 #12
@donbock
First of all I want to acknowledge the help they have given me the expert and the moderator donbock and jkmyoung by their knowledge, the first theoretical and the second for his specific example, both responses have the qualification the best answer, sinseramente my deepest thanks to the two and also would like to congratulate the person or persons had the idea to create this website invaluable for people like me can find help in this beautiful world that is the programming and compare it to the game of chess, because both with a little imagination can one achieve their goal. Thank you very much.
May 19 '10 #13
@jkmyoung
First of all I want to acknowledge the help they have given me the expert and the moderator donbock and jkmyoung by their knowledge, the first theoretical and the second for his specific example, both responses have the qualification the best answer, sinseramente my deepest thanks to the two and also would like to congratulate the person or persons had the idea to create this website invaluable for people like me can find help in this beautiful world that is the programming and compare it to the game of chess, because both with a little imagination can one achieve their goal. Thank you very much.
May 19 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ravi | last post by:
I recently had to write some code which required me to use a binary search tree (BST) due to running time concerns. I found that there is no BST class in the Standard C++ library. I have been told...
0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
1
by: Andy | last post by:
Say given the linearizer table and the key as tbl = {1, 10, 12, 25, 35, 47}; KEY = 1 Can Binary Search be used to find the enclosing segments? In this case KEY is enclosed by the segments 1 and...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
1
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child...
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
1
by: phanipep | last post by:
When is the binary search used for searching in a list? Give the binary search algorithm. Compare the performance of binary search with linear search?
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.