Checking whether two lists contain the same elements











up vote
1
down vote

favorite
1












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:




  1. Both lists contain four different numbers;

  2. 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:




  1. The probability to have four different numbers in both lists are about
    1/10;

  2. The probability to have for these two lists containing two same set
    of numbers are very small, about 10^(-11);

  3. I really have to do many loops.


Could someone help me out? I think a good idea can really boost the code.










share|improve this question
















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















up vote
1
down vote

favorite
1












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:




  1. Both lists contain four different numbers;

  2. 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:




  1. The probability to have four different numbers in both lists are about
    1/10;

  2. The probability to have for these two lists containing two same set
    of numbers are very small, about 10^(-11);

  3. I really have to do many loops.


Could someone help me out? I think a good idea can really boost the code.










share|improve this question
















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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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:




  1. Both lists contain four different numbers;

  2. 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:




  1. The probability to have four different numbers in both lists are about
    1/10;

  2. The probability to have for these two lists containing two same set
    of numbers are very small, about 10^(-11);

  3. I really have to do many loops.


Could someone help me out? I think a good idea can really boost the code.










share|improve this question















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:




  1. Both lists contain four different numbers;

  2. 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:




  1. The probability to have four different numbers in both lists are about
    1/10;

  2. The probability to have for these two lists containing two same set
    of numbers are very small, about 10^(-11);

  3. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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.






share|improve this answer





















  • 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













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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

















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.






share|improve this answer





















  • 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















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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'