3. 7.3.12 Professor T. R. S. Eighty proposes the following version of binary search: 1. binary - search2(s, i, j, key) 2. if (i > j) 3. return 0 4. k = ?(i + j)/2? 5. if (key == s_k) 6. return k 7. k1 = binary - search2(s, i, k - 1, key) 8. k2 = binary - search2(s, k + 1, j, key) 9. return k1 + k2 (a) (5 pts) Show that binary - search2 is correct; that is, if key is present, the algorithm returns its index, but if key is not present, it returns 0. (b) (5 pts) Find the worst case running time of binary-search2.
Added by Michael M.
Close
Step 1
binary_search2(s, i, j, key): - if i > j return 0 - k = floor((i + j)/2) - if key == s_k return k - k1 = binary_search2(s, i, k - 1, key) - k2 = binary_search2(s, k + 1, j, key) - return k1 + k2 Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 98 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Akash M.
Problem 3. Heap and heap property [15 pts] Provide tight bound for the running time of finding the largest element in binary min-heap with n elements? Justify your answer:
Sri K.
'Write down the algorithm for Binary search technique.'
Chandra J.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD