# What Makes an Algorithm Good

Computer Science I · Algorithmics · https://tryals.app/learn/computer-science-i/what-makes-an-algorithm-good

## Beyond It Works

An **algorithm** is a finite, well-defined sequence of steps solving a problem. Correctness and efficiency decide its true worth.

| Quality | Definition | Why It Matters |
| --- | --- | --- |
| **Correctness** | Right output for every valid input | Essential; wrong answers are useless |
| **Efficiency** | Growth of time/space via **Big-O** | Determines scale limits |
| **Clarity** | Readability and simplicity | Prevents implementation bugs |

An algorithm must be **finite** (it terminates) and **definite** (unambiguous steps).

We write in **pseudocode** to reason precisely without language syntax distractions.

## Correctness First

A common pitfall is optimizing for speed before establishing **correctness**. A fast algorithm giving wrong answers is worthless; a correct-but-slow one at least works.

**The Golden Rule:** Get it right first, argue or prove why every edge case is handled, and **only then** make it efficient. 

Premature optimization of an incorrect method wastes effort on the wrong thing. Always prioritize a verifiable solution over a clever, obscure one.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. The lesson says an algorithm must be finite and definite. Select every description that violates at least one of those two requirements.

A. A procedure written in pseudocode rather than Python
B. A procedure that may loop forever on some inputs
C. An instruction that says 'do whatever seems best'
D. A step that says 'pick a good value somehow'
E. A step that says 'if x > 0 print yes, else print no'

**Answer:** B. A procedure that may loop forever on some inputs; C. An instruction that says 'do whatever seems best'; D. A step that says 'pick a good value somehow'

**Why:** Looping forever breaks finiteness; 'somehow' and 'whatever seems best' break definiteness. Pseudocode and a clear if/else break neither, the lesson explicitly says algorithms are usually described in pseudocode.

Page: https://tryals.app/practice/computer-science-i/what-makes-an-algorithm-good/the-lesson-says-an-algorithm-must-be-finite-and-definite-select

### 2. An O(n log n) sort loses to an O(n²) insertion sort on a 20-element array, and every production sort library exploits this. What are they doing?

A. Using the O(n²) algorithm only when memory is constrained
B. Switching to insertion sort below a threshold, where constants win
C. Reducing the recursion depth to save stack space
D. Detecting already-sorted input and skipping the sort

**Answer:** B. Switching to insertion sort below a threshold, where constants win

**Why:** Constants are exactly what the notation discards, and at small n they dominate, which is why Timsort and libstdc++ both fall back to insertion sort under about 16 to 32 elements. Big-O answers a question about scaling, and using it to answer a question about speed is the error.

Page: https://tryals.app/practice/computer-science-i/what-makes-an-algorithm-good/an-o-n-log-n-sort-loses-to-an-o-n-insertion-sort-on-a-20-element

### 3. Match each quality or property to its meaning.

**Answer:**

- Correctness → Right output for every valid input, edge cases included
- Efficiency → How time and memory grow with the input size
- Finite → The procedure always terminates
- Definite → Every step is unambiguous

**Why:** Correctness and efficiency are qualities a good algorithm is judged on; finite and definite are the two requirements for something to be an algorithm at all.

Page: https://tryals.app/practice/computer-science-i/what-makes-an-algorithm-good/match-each-quality-or-property-to-its-meaning

### 4. Order the recommended design workflow, which puts correctness before speed.

**Answer:**

1. Work out precisely what the correct output is for every input
2. Write an algorithm and argue why it is correct
3. Measure how its cost grows with input size
4. Only then rewrite it to run faster

**Why:** Establish what is correct, build and justify a correct method, analyze its cost, and optimize last. Optimizing before correctness is the named pitfall.

Page: https://tryals.app/practice/computer-science-i/what-makes-an-algorithm-good/order-the-recommended-design-workflow-which-puts-correctness-before

### 5. A set of instructions counts as a genuine algorithm only if it is finite (it always stops) and definite (every step is unambiguous). Sort each description.

**Answer:**

- Is a valid algorithm: For each item in the list, swap it with its neighbour if larger, then stop, Double x, then subtract 3, then halt
- Not an algorithm: Keep guessing a number until it feels right, Repeat 'add 1 to x' forever

**Why:** A valid algorithm must be finite and definite. 'Guess until it feels right' and 'do something clever' are not definite, and 'repeat forever' is not finite. The others give an exact, terminating recipe.

Page: https://tryals.app/practice/computer-science-i/what-makes-an-algorithm-good/a-set-of-instructions-counts-as-a-genuine-algorithm-only-if-it-is
