Docs

advertisement

Array Searching

The applet on the left is a searching applet. You can create arrays and then search for values in the arrays using various search techniques.

Click here to watch a demo

Standard Buttons:

Create -- Creates an array of values. Enter a single number in the text box to specify the size of the array, enter a comma separated list into the text box to specify the contents of the array, or leave the textbox blank to create the default array.

Find -- Searches for a value in the array. Enter the value you seek in the textbox to search for a specific value, or leave blank to search for a random value.

Find All -- Searches for a value in the array and marks all occurences of the value. Enter the value to search for in the textbox to search for a specific value, or leave blank to search for a random value.

Clear -- Clears the array(s).

Submit -- Submits the current display of arrays.

Additional Options:

Unsorted/Linear -- The data will be unsorted and a linear search algorithm will be used to find a value in the array.

Sorted/Linear -- The data will be sorted and a linear search algorithm will be used to find a value in the array

Sorted/Binary -- The data will be sorted and a binary search algorithm will be used to find a value in the array.

Speed -- Controls the speed of the animation.

Show Code -- Shows a C++ implementation of the search algorithm. This option is only available when a single array type and algorithm is selected.

Highlighting:

The pink block indicates the value currently being checked.

The darker blocks indicate the value has already been checked.

The green blocks indicate an occurrence of the value.

The gray blocks indicate the blocks have been eliminated from the search.

Linear Searching

Linear searching operates by checking every element of a list, one at a time in sequence, until a match is found. Notice there is no requirement that the array be sorted.

Enter the size of the array in the box and selectCreate.

Now use the box to enter a value you would like to find. The ILM highlights the cells as they are searched.

Note the number of comparisons required.

Click Show Code to view code that implements a linear search.

#1 What is the Big O runtime of a linear search?

#2 Is the expected time different if you are searching for an item that is not there? How does it compare?

Sorted Linear Searching

If the array is sorted, linear search algortihms can save time by stopping once the value is clearly not in the array. It operates by checking every element of a list, one at a time, in sequence until a match is found or until the value found is greater than the value sought.

Check only the Sorted/Linear checkbox.

Enter the size of the array in the box and selectCreate.

Now use the box to enter a value you would like to find. Select an item which is not in the array. The ILM highlights the cells as they are searched.

Note the number of comparisons required.

Click Show Code to view code that implements a sorted linear search.

#1 What is the Big O runtime of a sorted linear search?

#2 Click both Unsorted/Linear and Sorted/Linear . How do the runtimes compare? Does that imply that one is faster than the other? Why or why not?

Binary Searching

A binary search algorithm is a technique for locating a particular value in a sorted list It closes in on the location of the sought value by selecting the middle element in the span, comparing its value to the target value, and determining if it is greater than, less than, or equal to the target value. A guessed index whose value turns out to be too high becomes the new upper bound of the span, and if its value is too low that index becomes the new lower bound.

A binary search only works when the array is sorted so select the Sorted/Binaryoption.

Enter the size of the array in the box and selectCreate.

Now use the box to enter a value you would like to find. The ILM highlights the cells which are examined. The cells which have been eliminated from the search are grayed out.

Note the number of comparisons required.

ClickShow Codeto see an implementaion of the binary search algorithm in c++ .

#1 What is the Big O runtime of a binary search?

Comparing Search Algorithms

After having looked at two search techniques, linear and binary searching, some students might wonder why we need to have both techniques. Let's compare and contrast the two and see if they are both necessary.

Select all three combinations to compare unsorted linear searches, sorted linear searches, and binary seaches side by side.

Create several different arrays and search for different values in them.

Note that due to space limitations you will not be able to see the entire array for each method. Using a size 65 or smaller will allow all cells to be seen simultaneously.

Also try finding all occurrences of a value.

#1 Are there any benefits to using an unsorted linear search instead of the other search techniques? If so, what are they? Hint, consider the time to add a new element to the table.

#2 Are there any benefits to using an sorted linear search instead of the other search techniques? If so, what are they?

#3 Are there any benefits to using a binary search instead of the other search techniques? If so, what are they?

#4 Fill in the following table. Note that while Big O throws out constants,

Expected Runtime allows you to be more precise in stating the time you expect on average. (Thus, you can differentiate between n and n/2).

Method

Searching Comparison

Expected

Runtime find unsorted linear find sorted linear find sorted binary

Big

0

Method

Searching Comparison

Expected

Runtime findall unsorted linear findall sorted linear findall sorted binary

Big

0

Download