July 2018
Beginner
202 pages
5h 4m
English
We want to determine the complexity of an algorithm checking for duplicates in an array by considering the best and worst case performance. Find the number of operations performed in the Snippet 1.4 for both the worst and best case. There is no need to work out the algorithmic complexity in big O notation. Assume that the inner loop results in eight operations every time it executes.
For the outer loop, assume four operations:
public boolean containsDuplicates(int[] numbers) { for (int i=0; i<numbers.length; i++) { for (int j=0; j<numbers.length; j++) { if (i != j && numbers[i] == numbers[j]) return true; } } return false;}
Read now
Unlock full access