Topic 02b Divide & Conquer SOLUTIONS.pdf - Topic 2b Divide Conquer Solutions Copyright(c 2017 Daniel D Suthers All rights reserved These solution

Topic 02b Divide & Conquer SOLUTIONS.pdf - Topic 2b...

This preview shows page 1 - 2 out of 3 pages.

Topic 2b, Divide & Conquer: Solutions Copyright (c) 2017 Daniel D. Suthers. All rights reserved. These solution notes may only be used by students in sections of ICS 311 Fall 2017 at the University of Hawaii. 1. Correctness of Binary Search Consider the Binary Search algorithm shown below (a Divide & Conquer algorithm). The inputs are a sorted array A of integers. You will use a loop invariant to show that the algorithm finds x correctly if it is in the range of A to be searched. Binary-Search ( x , A , min , max ) 1 low = min 2 high = max 3 while low <= high 4 mid = ( low + high ) / 2 5 if x < A [ mid ] 6 high = mid - 1 7 else if x > A [ mid ] 8 low = mid + 1 9 else return mid // since x == A [ mid ] 10 return “NOT FOUND” (1 pt) a. State the loop invariant for the while loop. Hint: Think about what the algorithm does and why you think it is correct. Second Hint: "If x is in A[min,max] then ..." what must remain true each pass? If x is in the range of the array A[min,max] to be searched it is in the subarray A[low,high]. b. ( Initialization ) Referencing the code, show the invariant is true at loop initialization: Trivially true because low=min and high=max in lines 1 and 2.
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read all 3 pages?

  • Fall '08
  • Barjaktarovic,M

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors