AP® Computer Science A · Unit 4 · Topic 4.2

Turning a Data Set into an Algorithm

A data set only becomes useful once you pick a specific question to ask it; that question tells you what to track as you move through the values one at a time in a loop.

A table of numbers sitting on a page does nothing by itself. It becomes useful the moment you ask it something: which value is biggest, how many pass a threshold, what's the average. That question decides what your algorithm has to track as it works. Without the question, there's no way to know what the loop should even be doing — so planning an algorithm always starts with pinning down exactly what you want to know, before a single line of code gets written.

Once the question is fixed, the data set turns into an algorithm through a simple pattern: a loop that visits each value once, updating one or more tracking variables along the way. Finding the warmest day means keeping a running maximum and comparing each new temperature to it. Counting values above average means first computing the average, then looping a second time and incrementing a counter whenever a value clears it. The table becomes a sequence the loop walks through index by index, and the tracking variables carry forward whatever answer the question demands.

This plan-first habit connects directly to arrays and ArrayLists, where the loop you sketch on paper becomes a for loop over indices or an enhanced for loop over elements. It also sets up search and sort algorithms later, since those are just more elaborate versions of the same idea: decide what you're looking for, then design the pass or passes over the data that find it. The size of the data set never changes this plan — a million values just means the same loop runs a million times instead of twelve.

Key terms

algorithm
A precise, step-by-step procedure that takes input data and produces a specific answer.
running maximum/minimum
A variable updated each loop pass to hold the largest or smallest value seen so far.
pass over the data
One complete loop through every value in the data set, often needed more than once for a single question.
tracking variable
A variable, such as a counter or running total, updated as the loop processes each value.

Watch it explained

  1. How it worksHow does a table of data turn into a loop that touches one value at a time?
  2. Worked exampleWalk me through planning an algorithm that finds the warmest day in a week of temperatures.
  3. Worked exampleFind the algorithm that counts how many of 12 monthly rainfall values beat the average.
  4. Going furtherWhat changes in the plan when the data set grows from 12 values to a million?

Check yourself

Your turn1 of 2
A data set has 36 values, and exactly 3 of them are above the data set's average. How many of the 36 values are NOT above the average?

End of topic.