What is a parameterized algorithm, and how does it improve code reusability and efficiency?

What is a parameterized algorithm, and how does it improve code reusability and efficiency?

From the BUET MSC (CSE) October 2026 – Written exam, question 11 of 14.

Answers (1)

A parameterized (generic) algorithm is written once in terms of parameters — the data type, a comparison rule, a size or a strategy — instead of being hard-coded for one case. Templates in C++ and generics in Java are the usual tools.

Reusability: one tested implementation serves many types and behaviours, for example a single sort for integers, strings or employee records, ordered by any comparator.

static <T> void sort(List<T> items, Comparator<T> order) { ... }

sort(names, Comparator.naturalOrder());
sort(employees, Comparator.comparing(Employee::salary));

Efficiency: less duplicated code to maintain and fewer bugs; with C++ templates the compiler generates specialised code for each type, so there is no run-time cost; tuning parameters (block size, threshold) lets one algorithm adapt to the input.

(In complexity theory the term also means fixed-parameter algorithms, running in f(k)·n^c time for a small parameter k, which makes some NP-hard problems practical.)

— rhs

More from this paper