Thanks for your helpful answer Consider the following method

Thanks for your helpful answer!

Consider the following method: What is the complexity of this method in big-O notation? Describe a better algorithm (in pseudo-code) to test an array for duplicates.

Solution

1.Complexity for given algo would be: O(n^2).

as this function has nested for loop ,outer loop will run n times and inner loop will get execute n-1 times ,

so Time complexity will be O(n*(n-1))==>O(n^2)

2. Yes better algorithm will be with Time Complexity O(n):

void hasDuplicates(int array[], int n)

{

  int i;

}

Thanks for your helpful answer! Consider the following method: What is the complexity of this method in big-O notation? Describe a better algorithm (in pseudo-c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site