![]() |
|
#1
|
|||
|
|||
searching between pairs in an arrayOk, 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
|
||||
|
||||
|
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:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
I had that idea before but i don't think it will workok 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
|
||||
|
||||
|
Quote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
|||
|
|||
Thanx WaltP, but still want it fasterThanx 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:
Last edited by admin : 26-Apr-2005 at 05:02.
Reason: please insert your example C codes between [c] and [/c] bbcode tags
|
|
#6
|
|||
|
|||
Another questionI 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
|
||||
|
||||
|
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
|
|||
|
|||
Whoops sorrySorry about that. I didn't really read that.
So have you got any ideas?? |
|
#9
|
|||
|
|||
|
There are different ways to go about sorting an array for instance try using selection-sort
|
|
#10
|
||||
|
||||
|
Quote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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