Click here to Skip to main content
15,914,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to Design and Analysis of Algorithms. I have a nested loop and and if statement.I am unable to determine the primitive operations being done in if statement. The statement is

for (i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j and A[i]==A[j])
duplicate=true
break;
if(duplicate)
break;

What I have tried:

i am determining the No of operations in if statement as follows:

Accessing array element 2 Times
comparing i and
Comparing A[i] and A[j]
Comparing AND Operator
all this is being done N times. Am i right in guessing the number of primitive operations in if statement? if not then please help me correct this. Thanks in advance
Posted
Updated 11-Mar-18 4:12am
v2

1 solution

This is fairly straightforward if we add some more spaces and parentheses:
C++
if ( (i != j) and (A[i] == A[j]) )
         v     v         v
         |     |         +-----> equality operator comparing two array objects
         |     +---------------> AND logical operator testing if both expressions are true
         +---------------------> inequality operator comparing i and j

- first test if i is not equal to j.
- if that is true then the AND operator means we must test the next expression
- test if array element A[i] is equal to A[j]
- If both are true then the whole expression is true
 
Share this answer
 
Comments
Jamsdhaid 11-Mar-18 9:53am    
@Richard Whether it will not cost for accessing the array elements?
then according to the upper Loops. what can be its complexity? Will you please make it clear? and what if the 1st condition is false?
Richard MacCutchan 11-Mar-18 10:03am    
What do you mean by "what can be its complexity"?
There are two loops: the outer one iterates through all the elements of the array. The inner loop does the same, and whenever the two indices are different the code compares the elements. If they are the same then it sets duplicate=true. However, that only tells you that you have one or more duplicates, not exactly how many or where they are.
Jamsdhaid 11-Mar-18 10:09am    
Sorry. it's not complexity. But i am trying to ask that whether it will not cost for Accessing the array as A[i] and A[j] where both i and j have different values.
if it will cost for Accessing the array than, what will be its Time Complexity.
[no name] 11-Mar-18 10:24am    
Looks like you are asking about short circuit Evaluation, which most Compilers do Support. You can read about it here: Short-circuit evaluation - Wikipedia[^]

In case i and j are the same i!= j becomes false and therefore the AND connected term will not be evaluated because the result is allready complete. So no A[i], A[j] Access in this case.
Jamsdhaid 11-Mar-18 10:31am    
What will be the Cost of the if Statement. Would it be n*n ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900