Analysis of Algorithms

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

  • Do the cities have to be in order?
    • No, the cities can be in any order. Linear search examines them one by one regardless of their order.
  • Does it hurt that "Albany" is in the array twice?
    • There are two answers to this:
      1. If the target is not "Albany", it makes no difference at all.
      2. If the target is "Albany", linear search finds the first one, and says nothing about the second. Depending on your application this might or might not be acceptable.

Analysis of Algorithms

We have (partially) analyzed the algorithm by looking it over and answering some questions about how it works. Professional programmers must be able to analyze algorithms in order to use them successfully. Many books have been written about the analysis of algorithms, and all university computer science departments have at least one course in the topic.

QUESTION 15:

What type of control structure do you suppose will be used to look through the cells of the array one by one (as required by linear search)?