Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigO calculation for duplicate finder #7

Open
sonerfromnurland opened this issue Jun 3, 2023 · 0 comments
Open

BigO calculation for duplicate finder #7

sonerfromnurland opened this issue Jun 3, 2023 · 0 comments

Comments

@sonerfromnurland
Copy link

sonerfromnurland commented Jun 3, 2023

I'm reading the BigO calculation on page 19, on which, for the worst-case scenario of the inner loop, n*(n*8) is given, but for the outer loop, 4n is given. If 4 is the operation count of the outer for loop (1 for assignment, 1 for comparison, and 2 for incremental), why don't we apply the same logic to the inner one? That is, 8*n for the inner loop and 4*n for the outer loop. Shouldn't the outcome be 32n^2?

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; // 8 operations
} }
  return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant