Searching

All computers organize information. To find information, computers must have ways of looking it up. We call this **searching**.

You are probably familiar with the concept of search - you type in keywords to Google, for example, and Google searches "the web" for your keywords. (Actually, Google searches their directory of the web for your keywords.)

In order to understand how computers do this, and do it efficiently, you will write some searching algorithms.

Exercise 1: Linear Search 1. Create an array with 10 ints in it, in any order. 2. Now, create a variable that will be the number that will be searched for. Write code to search the array for that item and print out the index in the array where it was found. If it was not found, -1 should be returned. 3. Move this code into a function with the following signature: code format="java" int search(int [] a, int item) { } code where a is the array to search and item is the number to find. Call this function several times using your array to check that it works and print out the result when it is returned.

4. Now create an array that contains 2000 items (fill it with random int values between 0 and 2000). 5. Call search on this array with values that are in the array and values that are not in the array. Is there any appreciable difference in the speed of the search?

This function is known as **Linear Search**. Why do you think that it is called this?

After you have completed the section on **recursion**​ come back here to learn about **​binary search.**