Checking whether two lists contain the same elements
up vote
1
down vote
favorite
I have two lists of numbers:
$${i_1, i_2, i_3,i_4}$$ and $${j_1,j_2,j_3,j_4}$$
The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do something I want, otherwise it just cycle the loop. The conditions are:
- Both lists contain four different numbers;
- If ignoring the ordering, in other words, we view these two lists as two set, then they are the same.
I want to find a very fast way to get the answer to the problem. I have very slow Fortran code as follows:
if (i2==i1) cycle
if (i3==i1) cycle
if (i3==i2) cycle
if (i4==i1) cycle
if (i4==i2) cycle
if (i4==i3) cycle
if (j2==j1) cycle
if (j3==j1) cycle
if (j3==j2) cycle
if (j4==j1) cycle
if (j4==j2) cycle
if (j4==j3) cycle
q1=0
q2=0
q3=0
q4=0
if (i1==j1)q1=1
if (i1==j2)q1=2
if (i1==j3)q1=3
if (i1==j4)q1=4
if (i2==j1)q2=1
if (i2==j2)q2=2
if (i2==j3)q2=3
if (i2==j4)q2=4
if (i3==j1)q3=1
if (i3==j2)q3=2
if (i3==j3)q3=3
if (i3==j4)q3=4
if (i4==j1)q4=1
if (i4==j2)q4=2
if (i4==j3)q4=3
if (i4==j4)q4=4
if (q1*q2*q3*q4==24)then
Something I want to do
endif
I think to find a good program, the following should also be considered as well as cutting down the number of instructions:
- The probability to have four different numbers in both lists are about
1/10; - The probability to have for these two lists containing two same set
of numbers are very small, about 10^(-11); - I really have to do many loops.
Could someone help me out? I think a good idea can really boost the code.
sorting complexity fortran
bumped to the homepage by Community♦ 31 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 3 more comments
up vote
1
down vote
favorite
I have two lists of numbers:
$${i_1, i_2, i_3,i_4}$$ and $${j_1,j_2,j_3,j_4}$$
The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do something I want, otherwise it just cycle the loop. The conditions are:
- Both lists contain four different numbers;
- If ignoring the ordering, in other words, we view these two lists as two set, then they are the same.
I want to find a very fast way to get the answer to the problem. I have very slow Fortran code as follows:
if (i2==i1) cycle
if (i3==i1) cycle
if (i3==i2) cycle
if (i4==i1) cycle
if (i4==i2) cycle
if (i4==i3) cycle
if (j2==j1) cycle
if (j3==j1) cycle
if (j3==j2) cycle
if (j4==j1) cycle
if (j4==j2) cycle
if (j4==j3) cycle
q1=0
q2=0
q3=0
q4=0
if (i1==j1)q1=1
if (i1==j2)q1=2
if (i1==j3)q1=3
if (i1==j4)q1=4
if (i2==j1)q2=1
if (i2==j2)q2=2
if (i2==j3)q2=3
if (i2==j4)q2=4
if (i3==j1)q3=1
if (i3==j2)q3=2
if (i3==j3)q3=3
if (i3==j4)q3=4
if (i4==j1)q4=1
if (i4==j2)q4=2
if (i4==j3)q4=3
if (i4==j4)q4=4
if (q1*q2*q3*q4==24)then
Something I want to do
endif
I think to find a good program, the following should also be considered as well as cutting down the number of instructions:
- The probability to have four different numbers in both lists are about
1/10; - The probability to have for these two lists containing two same set
of numbers are very small, about 10^(-11); - I really have to do many loops.
Could someone help me out? I think a good idea can really boost the code.
sorting complexity fortran
bumped to the homepage by Community♦ 31 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have two lists of numbers:
$${i_1, i_2, i_3,i_4}$$ and $${j_1,j_2,j_3,j_4}$$
The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do something I want, otherwise it just cycle the loop. The conditions are:
- Both lists contain four different numbers;
- If ignoring the ordering, in other words, we view these two lists as two set, then they are the same.
I want to find a very fast way to get the answer to the problem. I have very slow Fortran code as follows:
if (i2==i1) cycle
if (i3==i1) cycle
if (i3==i2) cycle
if (i4==i1) cycle
if (i4==i2) cycle
if (i4==i3) cycle
if (j2==j1) cycle
if (j3==j1) cycle
if (j3==j2) cycle
if (j4==j1) cycle
if (j4==j2) cycle
if (j4==j3) cycle
q1=0
q2=0
q3=0
q4=0
if (i1==j1)q1=1
if (i1==j2)q1=2
if (i1==j3)q1=3
if (i1==j4)q1=4
if (i2==j1)q2=1
if (i2==j2)q2=2
if (i2==j3)q2=3
if (i2==j4)q2=4
if (i3==j1)q3=1
if (i3==j2)q3=2
if (i3==j3)q3=3
if (i3==j4)q3=4
if (i4==j1)q4=1
if (i4==j2)q4=2
if (i4==j3)q4=3
if (i4==j4)q4=4
if (q1*q2*q3*q4==24)then
Something I want to do
endif
I think to find a good program, the following should also be considered as well as cutting down the number of instructions:
- The probability to have four different numbers in both lists are about
1/10; - The probability to have for these two lists containing two same set
of numbers are very small, about 10^(-11); - I really have to do many loops.
Could someone help me out? I think a good idea can really boost the code.
sorting complexity fortran
I have two lists of numbers:
$${i_1, i_2, i_3,i_4}$$ and $${j_1,j_2,j_3,j_4}$$
The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do something I want, otherwise it just cycle the loop. The conditions are:
- Both lists contain four different numbers;
- If ignoring the ordering, in other words, we view these two lists as two set, then they are the same.
I want to find a very fast way to get the answer to the problem. I have very slow Fortran code as follows:
if (i2==i1) cycle
if (i3==i1) cycle
if (i3==i2) cycle
if (i4==i1) cycle
if (i4==i2) cycle
if (i4==i3) cycle
if (j2==j1) cycle
if (j3==j1) cycle
if (j3==j2) cycle
if (j4==j1) cycle
if (j4==j2) cycle
if (j4==j3) cycle
q1=0
q2=0
q3=0
q4=0
if (i1==j1)q1=1
if (i1==j2)q1=2
if (i1==j3)q1=3
if (i1==j4)q1=4
if (i2==j1)q2=1
if (i2==j2)q2=2
if (i2==j3)q2=3
if (i2==j4)q2=4
if (i3==j1)q3=1
if (i3==j2)q3=2
if (i3==j3)q3=3
if (i3==j4)q3=4
if (i4==j1)q4=1
if (i4==j2)q4=2
if (i4==j3)q4=3
if (i4==j4)q4=4
if (q1*q2*q3*q4==24)then
Something I want to do
endif
I think to find a good program, the following should also be considered as well as cutting down the number of instructions:
- The probability to have four different numbers in both lists are about
1/10; - The probability to have for these two lists containing two same set
of numbers are very small, about 10^(-11); - I really have to do many loops.
Could someone help me out? I think a good idea can really boost the code.
sorting complexity fortran
sorting complexity fortran
edited Jul 1 at 2:53
Jamal♦
30.2k11115226
30.2k11115226
asked Jun 29 at 3:45
xjtan
93
93
bumped to the homepage by Community♦ 31 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 31 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04
|
show 3 more comments
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
The only way to reduce the number of operations is to sort one of the arrays. But 4 elements is hardly worth it.
Perhaps when adding the numbers to the array then add them in a sorted order, otherwise quicksort is good for arrays under 10 elements in size.
Performing a binary search on a sorted array would give you O(log n) time for each search - with O(n log n) time to compare two full arrays of the same size. Quicksort is O(n log n), with binary search that's roughly O(2[n log n]).
Adding items in a sorted order, then searching, would be a similar complexity: O(n log n) for adding 'n' items, and O(n log n) for searching through n items. I prefer this solution because it's one simple algorithm, and it's cleaner.
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The only way to reduce the number of operations is to sort one of the arrays. But 4 elements is hardly worth it.
Perhaps when adding the numbers to the array then add them in a sorted order, otherwise quicksort is good for arrays under 10 elements in size.
Performing a binary search on a sorted array would give you O(log n) time for each search - with O(n log n) time to compare two full arrays of the same size. Quicksort is O(n log n), with binary search that's roughly O(2[n log n]).
Adding items in a sorted order, then searching, would be a similar complexity: O(n log n) for adding 'n' items, and O(n log n) for searching through n items. I prefer this solution because it's one simple algorithm, and it's cleaner.
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
add a comment |
up vote
0
down vote
The only way to reduce the number of operations is to sort one of the arrays. But 4 elements is hardly worth it.
Perhaps when adding the numbers to the array then add them in a sorted order, otherwise quicksort is good for arrays under 10 elements in size.
Performing a binary search on a sorted array would give you O(log n) time for each search - with O(n log n) time to compare two full arrays of the same size. Quicksort is O(n log n), with binary search that's roughly O(2[n log n]).
Adding items in a sorted order, then searching, would be a similar complexity: O(n log n) for adding 'n' items, and O(n log n) for searching through n items. I prefer this solution because it's one simple algorithm, and it's cleaner.
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
add a comment |
up vote
0
down vote
up vote
0
down vote
The only way to reduce the number of operations is to sort one of the arrays. But 4 elements is hardly worth it.
Perhaps when adding the numbers to the array then add them in a sorted order, otherwise quicksort is good for arrays under 10 elements in size.
Performing a binary search on a sorted array would give you O(log n) time for each search - with O(n log n) time to compare two full arrays of the same size. Quicksort is O(n log n), with binary search that's roughly O(2[n log n]).
Adding items in a sorted order, then searching, would be a similar complexity: O(n log n) for adding 'n' items, and O(n log n) for searching through n items. I prefer this solution because it's one simple algorithm, and it's cleaner.
The only way to reduce the number of operations is to sort one of the arrays. But 4 elements is hardly worth it.
Perhaps when adding the numbers to the array then add them in a sorted order, otherwise quicksort is good for arrays under 10 elements in size.
Performing a binary search on a sorted array would give you O(log n) time for each search - with O(n log n) time to compare two full arrays of the same size. Quicksort is O(n log n), with binary search that's roughly O(2[n log n]).
Adding items in a sorted order, then searching, would be a similar complexity: O(n log n) for adding 'n' items, and O(n log n) for searching through n items. I prefer this solution because it's one simple algorithm, and it's cleaner.
answered Jun 30 at 1:54
user309290
1
1
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
add a comment |
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
I think for this problem of small size( by small I mean the size of the list is just four), the complexity theory is not very useful here since I am only concern with this particular small system, the thing really matters here is cutting the number of instructions.
– xjtan
Jun 30 at 14:07
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197465%2fchecking-whether-two-lists-contain-the-same-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are the ranges of the loops? Are those if statements at the inner-most loop, or are they located at the top level of the loop(s)?
– Kyle Kanos
Jun 29 at 14:12
"The probability to have four different numbers in both lists are about 1/10" This is a lottery?
– curiousguy
Jun 29 at 16:21
Hi @KyleKanos, thanks for asking, I modify the code in the question, hope it is clear this time.
– xjtan
Jun 30 at 14:03
@curiousguy, I am doing some extensive Monte Carlo simulation for a very rare event in clusters and the situation is very challenging.
– xjtan
Jun 30 at 14:05
@xjtan I don't understand the loop. Where are the i1,i2,etc values set? Rather than trying to post some partial code, can you instead post the actual code you're using in production? Otherwise, this question makes no sense.
– Kyle Kanos
Jun 30 at 15:04