Skip to main content

Algorithm for largest and smallest

Short answer

An algorithm is a finite, ordered set of well‑defined steps that solves a problem. To find largest and smallest, initialise both to the first element, then iterate through the list, updating the largest if a larger value appears and the smallest if a smaller value appears.

Define algorithm. Write an algorithm for finding largest and smallest values from a list.

Algorithm

An algorithm is a finite, ordered sequence of well-defined steps that solves a problem and ends after a limited number of steps. It should have input, output, definiteness, finiteness and effectiveness.

Algorithm to find the largest and smallest values in a list

Let the list be A with N elements.

  1. Start
  2. Read N
  3. Read the elements A[1] ... A[N]
  4. Set large = A[1] and small = A[1]
  5. Set i = 2
  6. Repeat steps 7 to 9 while i <= N
  7. If A[i] > large, set large = A[i]
  8. If A[i] < small, set small = A[i]
  9. Set i = i + 1
  10. Print large and small
  11. Stop

Example: for the list 6, 2, 9, 4, both large and small start at 6; then small becomes 2 and large becomes 9. Output: largest = 9, smallest = 2.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 2 questions →