Question

1. (U & G-required) [20 points] Consider the following algorithm. ALGORITHM Enigma(A[0.n -1]) //Input: An array A[0.n- 1] of integer numbers for i ← 0 to n _ 2 do forj ← i +1 to n _ I do if A[i] == Ali] return false return true a) [5 points] What does this algorithm do? b) [15 points] Compute the running time of this algorithm.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Algorithm Enigma(A)

for i 0 to n-2

for j=i+1 to n-1

if(A[i] == A[j])

return false;

return true

a. This algorithm is running 2 loops. Outer loop from i = 0 to n-2 and inner loop from i+1 to n-1. For every outer loop element, it is checking whether it is equal to any element from inner loop. So, this algorithm is calculating for the existence of duplicate elements. If there is any duplicate element, it is returning false, else it is returning true

b. Time complexity :

There are two loops.

Inner loop is running n-1 times, n-2 times, .....1 times respectively. So, it is running a total of Sigma(n-1) times which is (n-1)*n/2.

So, time complexity is O(N^2)

Add a comment
Know the answer?
Add Answer to:
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT