Question: Which code has the worst possible running time? A ) int Search ( int [ ] data, int x ) { int n = data.length

Which code has the worst possible running time?
A)
int Search(int [] data, int x)
{
int n = data.length();
for (int i =0; i < n-1; i++)
{
if(data(i)==x)
return i;
}
return -1;
}
B)
int Search(int [] data, int x)
{
int n = data.length();
int first =0;
int last = n-1;
int mid=(first + last)/2;
while(first<=last)
{
if(data(mid)== x){
return mid;
}
if(x > data(mid)){
first = mid +1 ;
}
else{
last = mid -1 ;
}
mid =(first + last)/2;
}
return -1;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!