Analyzing and Improving Searches
- EK 4.1.2A Languages for algorithms include natural language, pseudocode, and visual and textual programming languages.
- EK 4.1.2B Natural language and pseudocode describe algorithms so that humans can understand them.
- EK 4.2.4E Sometimes, more efficient algorithms are more complex.
- EK 4.2.4H Linear search can be used when searching for an item in any list; binary search can be used only when the list is sorted.
Brian wants to rename this page "Searching a List." --MF, 2/19/18
On this page, you will see how the characteristics of a list can affect the algorithm for searching in it.
Do we need this commented out text? --MF, 6/17/20
Searching in Unsorted Data
When searching for an item in unsorted data, you have to check every item until you find the value you are looking for.
-
Build a
position of number in unsorted list
block that reports the earliest location of a number in an unsorted list or reports "Not In List" (or NIL, if you like) if the number is not in the list.
Searching unsorted data can become slow if we're dealing with a lot of data, but it is quick when working with short lists.
Searching in Sorted Data
If you are searching for a number in a sorted list, you have more information. Suppose you have a sorted list and you want to locate the position of a number.
Double-check this refernce with BH after U8 settles down. --MF, 6/17/20
Typically, sorted lists are from lowest to highest. You'll create a block that sorts a list of numbers in Unit 8,
Sorting a List.
Morgan: If the list is sorted, we could look at the middle element of the list and see if our number is in the first or last half of the list. That would save time.
Jasmine: This feels a lot like the guessing game we built—I pick a number and the computer tries to guess it by splitting the list in half with each guess.
Morgan: But in that one, the computer guessed a number that we picked. Now we want it to find a number in a list and report its position. It should work like this:
Jasmine: Yes, but it still feels the same. It should have the same guts as the guessing game.
Jasmine's last remark is a version of
abstraction. Jasmine noticed that
position in sorted list
and the
number guesser algorithm should have the same overall
structure.
-
Make a table that tracks the number of guesses it takes to find a number in a sorted list depending on the length of the list if that number turns out to be the last item.
Length of List |
Number of Guesses |
3 |
|
7 |
3 |
15 |
|
63 |
|
127 |
|