Pseudocode — a clear, language-agnostic plan for algorithms
Pseudocode is a way to describe the steps of an algorithm in plain, structured language without using the exact syntax of a programming language. Its goal is clarity: to communicate intent and logic so another person (or you later) can convert it into working code.
When to use pseudocode
- Designing algorithms before coding.
- Explaining logic in interviews, classrooms, or documentation.
- Focusing on structure and correctness instead of language details.
Simple rules and style guidelines (step-by-step)
- Be language-agnostic: avoid language-specific constructs unless needed.
- Use descriptive names for variables and functions (e.g., 'index', 'target', 'sum').
- Use consistent control keywords: IF, ELSE, WHILE, FOR, FOR EACH, FUNCTION, RETURN.
- Indent to show blocks and nesting — readability matters.
- Aim for high-level steps; skip low-level implementation details (e.g., don’t show pointer arithmetic if not needed).
- Keep it short but complete enough to implement unambiguously.
Common pseudocode keywords (you can adapt capitalization)
IF ... THEN ... ELSE, FOR i = ... TO ..., WHILE condition, REPEAT ... UNTIL, FUNCTION name(params), RETURN value, BREAK, CONTINUE
Examples
1) Linear search (find target in array)
FUNCTION LinearSearch(array, target)
FOR i = 0 TO length(array) - 1
IF array[i] == target THEN
RETURN i // index where found
END IF
END FOR
RETURN -1 // not found
END FUNCTION
2) Binary search (array must be sorted)
FUNCTION BinarySearch(array, target)
left = 0
right = length(array) - 1
WHILE left <= right
mid = (left + right) // 2
IF array[mid] == target THEN
RETURN mid
ELSE IF array[mid] < target THEN
left = mid + 1
ELSE
right = mid - 1
END IF
END WHILE
RETURN -1
END FUNCTION
3) Factorial (iterative and recursive)
// iterative
FUNCTION FactorialIter(n)
result = 1
FOR i = 2 TO n
result = result * i
END FOR
RETURN result
END FUNCTION
// recursive
FUNCTION FactorialRec(n)
IF n <= 1 THEN
RETURN 1
ELSE
RETURN n * FactorialRec(n - 1)
END IF
END FUNCTION
4) Bubble sort (simple sorting example)
FUNCTION BubbleSort(array)
n = length(array)
FOR i = 0 TO n - 2
FOR j = 0 TO n - 2 - i
IF array[j] > array[j + 1] THEN
SWAP array[j] and array[j + 1]
END IF
END FOR
END FOR
RETURN array
END FUNCTION
Translating pseudocode to real code
Converting is straightforward: map keywords to language syntax, replace comments with proper comment markers, and implement any omitted low-level details. For example, 'FOR i = 0 TO n - 1' becomes 'for (int i = 0; i < n; i++)' in C-like languages or 'for i in range(n):' in Python.
Checklist for clear pseudocode
- Are variable and function names descriptive?
- Is control flow obvious from indentation and keywords?
- Does it omit unnecessary low-level details?
- Could someone implement it in any language without guessing intent?
Common pitfalls
- Being too language-specific (using syntax only in one language).
- Being too vague (omitting critical steps).
- Mixing implementation optimizations too early — first get a clear correct version.
Practice exercises (try writing pseudocode)
- Write pseudocode to reverse a string in place.
- Write pseudocode to merge two sorted lists into one sorted list.
- Write pseudocode to detect a cycle in a linked list (Floyd’s algorithm).
Quick tips
- Keep examples short — they should show intent, not every micro-detail.
- When in doubt, add a brief comment to clarify tricky steps.
- Be consistent: choose capitalization and stick with it.
With practice, writing pseudocode will become a fast way to plan and explain algorithms. Start by writing pseudocode for small problems, then translate into code and test — that cycle will improve both your algorithm design and your ability to communicate it.