The midpoint of the current list is now the 13th

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: of the binary search algorithm in pseudocode: procedure binary search (x: integer, a1, a2, …, an: increasing integers) i := 1 {i is the left endpoint of interval} j := n {j is right endpoint of interval} while i < j m := ⌊(i + j)/2⌋ if x > am then i := m + 1 else j := m if x = ai then location := i else location := 0 return location http://www.cs.armstrong.edu/liang/animation/BinarySearchAnimation.html Binary Search Example: 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 1.  The list has 16 elements, therefore the midpoint is 8. The value in the 8th position is 10. Since 19 > 10, further search is restricted to positions 9 through 16. 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 2.  The midpoint of the list (positions 9 through 16) is now the 12th position with a value of 16. Since 19 > 16, further se...
View Full Document

This document was uploaded on 02/27/2014 for the course CS 215 at SIU Carbondale.

Ask a homework question - tutors are online