0% found this document useful (0 votes)
5 views2 pages

Year9 Unit1 Lesson7 Worksheet1

The document outlines four solutions for performing binary searches on a sorted list of numbers. Each solution describes a step-by-step method for narrowing down the search range until the desired number is found. The solutions emphasize the importance of sorting the list and systematically eliminating portions of the list based on comparisons with the guessed number.

Uploaded by

isestudents.ict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Year9 Unit1 Lesson7 Worksheet1

The document outlines four solutions for performing binary searches on a sorted list of numbers. Each solution describes a step-by-step method for narrowing down the search range until the desired number is found. The solutions emphasize the importance of sorting the list and systematically eliminating portions of the list based on comparisons with the guessed number.

Uploaded by

isestudents.ict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Year 9, Unit 1

Lesson 7
Worksheet 1
Binary searches

Solution 1

1 Sort the numbers.


2 Set min = 1 and max = n.
3 Estimate the mean of max and min.
4 Round the mean down so that it is a whole number (integer).
5 If this is the number you guessed, stop.
6 If your guess was too low, set min to be one larger than your guess.
7 If your guess was too high, set max to be one smaller than your guess.
8 Go back to step 2.

Solution 2

1 Sort the numbers in order.


2 Guess a number near the middle of the list.
3 If you did not find the number:
(a) bin half of the list (if the guess was high, bin the upper half of the list. If the guess was low,
bin the lower half of the list)
(b) guess a number near the middle of the new list
(c) repeat step 3 (a, b, and c) until the correct number is found.

Solution 3

1 The list should be ordered.


2 Select the middle number of the list.
3 If the number is equal to the one looked for, stop.
4 If the number is larger than the middle number, repeat steps 2 and 3 with the new list to the
right.
5 If the item is smaller than the middle number, repeat steps 2 and 3 with the new list to the left.
6 Repeat steps 4 and 5 until the number is found.

© Pearson Education Ltd 2019. Copying permitted for purchasing institution only. This material is not copyright
free.
Solution 4
OUTPUT “Which number do you want to find?”
INPUT user inputs 56
STORE the user’s input in the guess variable
guess_found = False
(we need to create a flag that identifies if the number is found)
WHILE guess_found = False:
Find midpoint of list
IF guess = record at midpoint of list THEN
guess_found = True
ELSE IF guess comes before the midpoint THEN
throw away the second half of the list
ELSE
throw away the first part of the list
OUTPUT guess

© Pearson Education Ltd 2019. Copying permitted for purchasing institution only. This material is not copyright
free.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy