Suppose you are looking for the nine of diamonds in a deck of cards. One approach to finding this card (which will work regardless of the order that the cards are in) is to look at each card one at a time until you find the card you want. Eventually you will find your card if it is there. This approach can be extremely time consuming if you are searching through a large number of things. It is reasonably easy to program though. This is called a sequential search.
|
Generates output of:
The value -20.7 is located at position 4 in the array. The value 101.1 is not located in the array. |
![]() |
Why do the comments in the code above call this a non-optimal sequential search? |
![]() |
Suppose that the nums array had been defined as: double[] nums = {1.1, -9.8, 4.5, 6, -20.7, -18.2, 5.1, 25.25, 19.3, 0.3, -20.7, 18.3}; What would seqSearch(nums, -20.7) return? |
![]() |
If the array being searched has 1000 items: (a) what is the maximum number of items in the list that need to be checked? (b) what is the minimum number of items? |
Here is an improved version of sequential search that stops when you find the item you are looking for.
|