---
title: "Algorithm for largest and smallest"
subject: "Computer Programming"
chapter: 2
key: "semester_1/computer_programming:define-algorithm-largest-smallest"
source: https://www.bctengineer.com/q/semester_1/computer_programming/define-algorithm-largest-smallest
site: BCT Engineering
---

# Algorithm for largest and smallest

Computer Programming · Chapter 2: Problem solving using Computer

- Asked in: 2068 Shrawan, 2+6 marks
- Video: [Find maximum element in an array (Largest element)](https://www.youtube.com/watch?v=y1bUfuTkmic) — Vivekanand Khyade - Algorithm Every Day
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/define-algorithm-largest-smallest
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-2

## Question

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

## 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.

## Answer

#### 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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. An algorithm is
   A. a finite, ordered sequence of well-defined steps that ends after limited steps
   B. a program with infinite loop
   C. a flowchart symbol
   D. a compiler
   Answer: A. Finiteness is a key property. (Key point: Algorithm definition)

2. In the algorithm, large and small are initially set to
   A. 0
   B. A[1]
   C. A[N]
   D. N
   Answer: B. Both start from the first element. (Key point: Initialization)

3. The loop starts at i = 2 because
   A. arrays start at 2
   B. A[1] is invalid
   C. the first element was already used for initialisation
   D. i = 1 would skip the list
   Answer: C. No need to compare A[1] with itself. (Key point: Loop start)

4. For 6, 2, 9, 4 the output is
   A. largest 6, smallest 4
   B. largest 9, smallest 4
   C. largest 6, smallest 2
   D. largest 9, smallest 2
   Answer: D. Max is 9, min is 2. (Key point: Result)

## Related in Computer Programming

- [Pass array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-largest-smallest) — Computer Programming, chapter 7
- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Algorithm and flowchart for largest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-flowchart-largest-n) — Computer Programming, chapter 2

## Related in other subjects

- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
- [Largest of four integers processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-largest-of-four) — Embedded System, chapter 2
- [Prim's algorithm with example](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aprim-algorithm-mst) — Data Structure and Algorithm, chapter 10, asked 2×
- [Secant method algorithm](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asecant-method-algorithm) — Numerical Method, chapter 2, asked 2×
