Question

Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return...

  1. Look at the following pseudo code:

Binary_search(int a[], int size)

{

……….// binary search and return

}

Selection_Sort(int a[], int z)

{

…..// do the selection sort

}

main()

{

    Selection_Sort(array, size);

    Binary_Search(array, item);

}

Calculate the time complexity of this code and use O() notation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution - Given the following pseudo code -

Binary_search(int a[], int size){
// binary search and return
}

Selection_Sort(int a[], int z){
// do the selection sort
}

main(){
Selection_Sort(array, size);
Binary_Search(array, item);
}

Let us assume that size of array be n i.e size = n

We know that the worst case time complexity of Selection sort is O(n2) and the worst case time complexity of Binary search is O(logn).

In main(), first selection_sort() is called to sort the array then binary_search() is called to search for the item in sorted array.

So, the overall complexity of this code is sum of both complexities

Over all complexity = O(n2) + O(logn)

The algorithm as a whole has a worst case running time of O(n2).

Add a comment
Know the answer?
Add Answer to:
Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT