GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2005, 00:21
asagohan asagohan is offline
New Member
 
Join Date: Apr 2005
Posts: 9
asagohan is on a distinguished road

searching between pairs in an array


Ok, I have a huge array of up to 100,000 integers (N), and i have a huge amount of pairs which can be up to N^2 (M). What i have to do is, read the ints into an array and find the largest number between each pair of indicies. It is easy to do it by brute force, just go through each element of the array between the pair of numbers for each pair. But this method takes a very long time for such a large data set. So I was hoping that someone could give me any ideas for a more efficient method. (The array would have to be pre processed in some way before hand i think).
  #2  
Old 20-Apr-2005, 00:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
If the data cannot be sorted, brute force is of course the easiest.

Another option requires another array of the same size. Sort the original array and the second array contains the index of each corresponding element's original index. For example:
Code:
orig 1 5 4 2 7 3 sort 1 2 3 4 5 7 indx 0 3 5 2 1 4
Now, start at the last entry in indx and search backwards for the first value that fits between your pair of indicies. The corresponding value from sort is your highest value. Or sort them descending, and start at the beginning....
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 21-Apr-2005, 08:20
asagohan asagohan is offline
New Member
 
Join Date: Apr 2005
Posts: 9
asagohan is on a distinguished road

I had that idea before but i don't think it will work


ok I had that idea ages ago but if i had 100,000 integers (N) and if i was to start at the end of the new array and search backwards, i would pretty much be doing a linear search for each pair each time right? Since the number of pairs is likely to be (N^2) then it will still take a REALLY long time because it is going through say at least half the array each time isnt it?
  #4  
Old 21-Apr-2005, 23:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by asagohan
ok I had that idea ages ago but if i had 100,000 integers (N) and if i was to start at the end of the new array and search backwards, i would pretty much be doing a linear search for each pair each time right? Since the number of pairs is likely to be (N^2) then it will still take a REALLY long time because it is going through say at least half the array each time isnt it?
Not necessarily. Sometimes it will be faster, sometimes slower. If the span is short, brute force will probably be faster. But if the span is wide, you have a better chance of one of the larger numbers between the span. I don't know the equation for figuring it out, but finding the statistical crossover point between the two methods allows you to choose which method to use given the parameters.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 24-Apr-2005, 01:38
asagohan asagohan is offline
New Member
 
Join Date: Apr 2005
Posts: 9
asagohan is on a distinguished road

Thanx WaltP, but still want it faster


Thanx WaltP, your method worked quite well. I was thinking to try this method to make it even faster, but it is only a little bit faster. I was thinking this is because the way i am pre-processing the array is too slow. What im doing is is looking for the next biggest number after each array index and then storing that number in the current index, so i can effectively jump across the array in different size leaps. But because i am lineraly going throught the array, i am losing time in the function. Any ideas??

CPP / C++ / C Code:
void function1(int * A, FILE * inp, int N)
{
	//use dynamic allocation for array B so it goes on the heap instead of stack malloc(N*sizeof(int));
	int B[N];
	int count=0;
	int pos=0;
	int j=0;
	int k=0;
	int origj;

	while(count<N)
	   	{
		pos=count;
		
			if(pos==N-1)
			{
				B[pos]=pos;
				break;
			}
			
			while(A[pos]>=A[count+1])
			{
				count++;
				
				if(count==N)
				{					
					break;
				}
				
			}
			
			if(count<N)
				B[pos]=count+1;
			else
				B[pos]=count;
			
			count=pos+1;
			
		}

		//printarray(B, N);
		
		count=0;
		while(fscanf(inp, "%i", &j)==1&&fscanf(inp, "%i", &k)==1)
		{
		origj=j;
			while(B[j]<=k)
			{
				j=B[j];
				
				if(j==B[j])
				{
					break;
				}
			}
			
			//printf("%i %i = %i\n", origj, k,j);
		}
			 
}
Last edited by admin : 26-Apr-2005 at 05:02. Reason: please insert your example C codes between [c] and [/c] bbcode tags
  #6  
Old 25-Apr-2005, 06:06
asagohan asagohan is offline
New Member
 
Join Date: Apr 2005
Posts: 9
asagohan is on a distinguished road

Another question


I was also thinking of using your idea but instead of using a normal array, i was thinking of using a heap as i am hoping to get the search done in log(n) time. But the problem is i am not sure how to organinse the heap to get this result. Can you help me with that please?
  #7  
Old 25-Apr-2005, 16:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Read the sticky at the top of the forum!!
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 26-Apr-2005, 04:10
asagohan asagohan is offline
New Member
 
Join Date: Apr 2005
Posts: 9
asagohan is on a distinguished road

Whoops sorry


Sorry about that. I didn't really read that.
So have you got any ideas??
  #9  
Old 26-Apr-2005, 10:31
clander clander is offline
New Member
 
Join Date: Apr 2005
Posts: 11
clander is on a distinguished road
There are different ways to go about sorting an array for instance try using selection-sort
  #10  
Old 26-Apr-2005, 13:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by asagohan
I was also thinking of using your idea but instead of using a normal array, i was thinking of using a heap as i am hoping to get the search done in log(n) time. But the problem is i am not sure how to organinse the heap to get this result. Can you help me with that please?
Please define the difference between a heap and an array. From your question I don't think you understand something.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 02:11
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 22:52
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 12:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:11.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.