473,396 Members | 1,766 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,396 software developers and data experts.

Algorithm to compare to arrays of int

Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return
true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu.
The memory size is not a problem.

/Dev
Nov 14 '05 #1
8 1749

"Devrobcom" <de*******@hotmail.com> wrote in message
news:40********@news1e1.seinf.abb.se...
Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu. The memory size is not a problem.


If speed is an issue, then consider using a hashtable. Have the hashtable
store the int as your index and a char as the data. If the data returned is
non-null then your number is located in the hashtable. You must remember to
remove items from the hashtable this way tho.
Allan
Nov 14 '05 #2
Devrobcom wrote:

Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return
true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu.
The memory size is not a problem.

/Dev


You imply that you program has knowledge when
a position in the "large" array is updated.
Why not, for each position changed in this "large"
array, search the "small" array for a match.

Since you haven't mentioned how the "large" array
is updated (round-robin, etc.), doing the above
may eliminate the need for keeping the "large"
array sorted (if its only purpose is for a later
binary search). Saving a 1000 item sort has gotta
provide a performance boost...

HTH,

Stephen
Nov 14 '05 #3
Hashing seems to be the only way to make this really fast IMHO.
"Devrobcom" <de*******@hotmail.com> wrote in message
news:40********@news1e1.seinf.abb.se...
Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu. The memory size is not a problem.

/Dev

Nov 14 '05 #4
On Tue, 18 May 2004 10:40:08 +0200, "Devrobcom"
<de*******@hotmail.com> wrote:
Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return
true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu.
The memory size is not a problem.


If the range of integers in large_array is reasonably constrained, you
could use a bit to represent each possibile value, making setting and
checking very fast. If the range is not limited, create a hash table.

--
Sev
Nov 14 '05 #5
Devrobcom wrote:
Hi
I have an array with 30 int and another array with 1000 int.
If any of the 30 intergers can be found in the 1000 int array I shall return
true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.

One way could be to:

bool any_match(int smal[], int smal_sz, int large[], int large_sz)
{
for(int i = 0; i < smal_sz; i++)
{
if ( binary_search(large, large_sz, smal[i]) >= 0) return true;
Is binary_search a standard C library function?
}
return false;
}

void my_main_thread()
{
//
int large_array[1000];
int smal_array[30]

//
// sort large array when updated
//

// check if any id can be found
if (any_match(smal_array, 30, large_array, 1000) )
{
dowork();
}
}

Since I will do this very frequently I need something that takes little cpu.
The memory size is not a problem.


It depends how often your arrays are updated compared to how
often you do the match. So, could you give details about how
many items at a time and how often you update each array...
Also interesting to know might be the sizeof the ints (or the
range of values); small int/range might 'allow' other strategies.

Kees

Nov 14 '05 #6
Devrobcom wrote:
I have an array with 30 int and another array with 1000 int.
If any of the 30 integers can be found in the 1000 int array
I shall return true.
The contents of the arrays will also change over time.
I'm looking for a way to do this with good performance.


comp.programming could probably help you out.

Nov 14 '05 #7
"Case" <no@no.no> wrote in message
news:40***********************@news.xs4all.nl...
Is binary_search a standard C library function?


No, but bsearch() is.

Alex
Nov 14 '05 #8
Thank you all!
I have been away from my computer the whole day and haven't been able to
join.
I will check if I can use a hash table.
/Dev
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Wahoo | last post by:
Dear All, What is the name of sorting algorithm used in list (STL) class? and how it work? Thanks!!! Best Regards, Wahoo
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
10
by: sulekhasweety | last post by:
Hi , this is the program showing implementation of a* algorithm, given n integers and a sum m ,write a program to find the set of integers summing to m using a* algorithm. but i am not...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.