- 2068 Shrawan · 2+6 marks
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.
- Start
- Read
N - Read the elements
A[1] ... A[N] - Set
large = A[1]andsmall = A[1] - Set
i = 2 - Repeat steps 7 to 9 while
i <= N - If
A[i] > large, setlarge = A[i] - If
A[i] < small, setsmall = A[i] - Set
i = i + 1 - Print
largeandsmall - 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)
Related questions
- Pass array; largest and smallest · Computer Programming, chapter 7
- Pass 2D array; largest and smallest · Computer Programming, chapter 7
- Algorithm and flowchart for largest · Computer Programming, chapter 2
- 8086 ALP: largest and smallest, display · Microprocessor, chapter 3
- Largest of four integers processor · Embedded System, chapter 2
- Prim's algorithm with example · Data Structure and Algorithm, chapter 10 · asked 2×
- Secant method algorithm · Numerical Method, chapter 2 · asked 2×