Module 3- Searching and Sorting Searching for Information Using a Computer
Why do Computers Need to Perform Searches? Some Examples Bank tellers need to look up the balance for a customer’s account number Mobile Phones can query the amount of credit remaining. On-line music stores are able to find the songs sung by a given artist.
How do Computers Perform Searches? The Method Get a Search Key For Example Phone Number Bank Account number Song Artist/ Song title Locate Information Related to the Search Key For Example Amount of Credit Remaining Account Balance Songs available for download sung by that Artist
Types of Searches? Some Examples Game 1 The linear Search Go straight through a list Game 2 Binary Search Divide and Conquer Game 3 Keys in a hash tables are associated with values
Linear Search Computers work very quickly. How efficient is a linear search? E.g. Imagine starting at the first page of the phone book and checking every page until you find a phone number for a man called John Williams.
Binary Search Your items must be sorted in order for a binary search to work. It is similar to how people locate information in a dictionary. In the phone book example, a book with 1 million names can now be searched with a maximum of 20 probes, which might take two hundredths of a second— hardly noticeable.
Number of Items in a listNumber of comparisons required in a binary search , ,048, ,073,741,82430 Binary Search
Game 3 - Hashing Data is stored at a specific location based on a hash function. Our game used a hash function to identify a column number for a ship. This column number (Hash Key) was created using the final digit of the sum of the digits of a ship’s number. As you have seen, it was slow to find the right ship in a chosen column if there was a large number of ships in a column. However, it was very quick if there was just one ship in a column. Hashing can be used for searching through lists of unsorted information. For example the Hong Kong Book. = 7 = 14