in Algorithms retagged by
790 views
1 vote
1 vote

Can anyone explain me the answer in detail?

in Algorithms retagged by
790 views

4 Comments

In first two passes we will get the larger elements at the end of the array so it wil take  (n-1)+(n-2) comparisons.

(n-1)+(n-2)=O(n)+O(n).

If the condition is not satisfying the first pair i.e last two elements after two passes then the condition wil never be satisfied for the remaining pairs .So we can stop the bubble sort after two passes.
1
1
for(i=0;i<2;i++){
    for(j=0;j<n;j++){
        .......
    }
}
1
1
thanks
0
0

1 Answer

0 votes
0 votes
The answer is O(n) as we just need to run one loop and check for condition x+y>100 condition.There is no need to sort the array we just need to meet the x+y>100 condition.Option A is correct.Please correct me if I am wrong.
by

Related questions

1 vote
1 vote
1 answer
3