Sonith Manakkat

Algorithms, part 12 of 128 min read11 visuals

Choosing the right algorithm

A decision cheat sheet that turns the whole series into questions you can ask about a problem, plus the practical advice experts give about measuring, clarity and using libraries.

  • Start from the question you are asking, then match it to a family of algorithms
  • Data size and real measurements decide, not cleverness
  • Clear code and good libraries beat early optimisation

Share on LinkedIn

Share
Reading level

Eleven parts in, you have met many algorithms. The skill that remains is choosing: looking at a problem and knowing which family it belongs to. This final part is a cheat sheet. Read the problem as a question, find the question below, and take the good first choices. Each row says why, so you can reason about cases the sheet does not cover.

Ask the question first

  1. 1Protecting data, or storing passwords?Yes Vetted encryption or slow salted hashing from a trusted libraryNo keep going
  2. 2Links between things, such as routes?Yes Graph search: BFS, DFS or DijkstraNo keep going
  3. 3Exact lookups by key, many times?Yes Hash tableNo keep going
  4. 4Most urgent item first?Yes Heap or priority queueNo keep going
  5. 5Best answer from repeating subproblems?Yes Dynamic programmingNo keep going
  6. 6Just need items in order?Yes The built-in sortNo keep going
  7. 7OtherwiseLinear search or the simplest code, then measure

Takeaway. Name the job first, then pick the family that matches it.

A first-pass guide to the right family of algorithm (details in the cheat sheets below).
  1. What is the job?Find, order, relate, choose, rank, learn or protect
  2. How big is the data?Ten items or ten million changes the answer
  3. What matters most?Speed, memory, simplicity or predictability
  4. Is there a library?A tested built-in usually beats your own
Four questions to ask before reaching for any algorithm.

The cheat sheet: finding things

QuestionGood first choicesWhy
Find in a small or unsorted listLinear searchNothing to prepare, and cheap when the list is short
Find in sorted data, many lookupsBinary search, or a hash tableBinary search takes about log n steps; hashing is fast on average
Find by exact keyHash tableAverage constant-time lookup, no order needed
Find by range, or walk in orderBalanced tree, or sorted arrayKeeps order, so ranges and neighbours are easy

A quick way to read the rows above: if the data is small, almost anything works, so pick the simplest. If you will look things up many times, pay once to organise the data (sort it, or build a hash table) and then enjoy fast lookups. Organising costs time up front and memory, so it only pays off when the number of lookups is large.

Expert viewTrade-offs behind the lookup choices

Hash tables give constant-time lookups on average, but they have no order and their worst case is worse when many keys collide. Binary search needs sorted data held in an indexable form, and inserting into a sorted array is costly, which is why dynamic data often uses a balanced tree, giving about log n time for lookup, insertion and in-order walks. The right choice depends on the mix of operations: mostly reads, mostly writes, or range queries.

Putting things in order

QuestionGood first choicesWhy
Just need it sortedYour language's built-in sortWell tested and tuned; many libraries use hybrids of the classic ideas
Tiny or nearly sorted dataInsertion sortVery little overhead, close to linear on nearly sorted input
Equal items must keep their orderMerge sort (a stable sort)Stable, with steady n log n time, at the cost of extra memory
Memory tight, good average speedQuick-style sortIn place and fast on average, but not stable and has a bad worst case

Relationships, choices and priorities

QuestionGood first choicesWhy
Network: fewest stepsBFSExplores level by level, so the first arrival uses the fewest steps
Network: visit or explore allDFSGoes deep, simple and low in memory
Network: cheapest weighted routeDijkstra (A* with estimates)Finds least cost when costs are not negative; estimates focus the search
Best answer, greedy choice is provably safeGreedyFast and simple when the proof holds
Best answer, overlapping subproblemsDynamic programmingSolve each small case once and reuse it
Best answer, otherwiseBacktracking with pruningTry options, abandon dead ends early
Most urgent firstHeap, or priority queueAlways gives the best item quickly as items arrive
Expert viewGreedy needs a proof

A greedy method makes the best-looking choice at each step. It is only correct when the problem has a property that makes this safe, and that has to be shown, not hoped for. Coin change shows the danger: with coins of 1, 3 and 4, a greedy pick for 6 gives 4+1+1 (three coins) while 3+3 needs two. When the greedy choice cannot be justified and subproblems repeat, dynamic programming is the dependable route. Where neither fits, search with pruning is the fallback, and it may be exponential in the worst case.

Ranking, learning and protecting

Notice how these rows differ from the earlier ones: here the right answer depends on data and context more than on a fixed recipe, so measure the outcome and not only the speed.

QuestionGood first choicesWhy
Recommend or rankScoring and similarityGive items a score from signals, then sort by it
Learn from dataMachine learningPatterns are learned from examples when rules are hard to write
Protect dataVetted encryption librariesNever write your own cryptography
Fingerprint or compare dataHashingShort, fixed-size, changes if the data changes
Store passwordsSlow, salted hashes from a trusted libraryPasswords are never kept, only checked

The series at a glance

PartCore ideaReach for it when
Big-ODescribing how work growsComparing options before building
SortingPutting items in orderYou need order or want faster lookups
SearchingFinding itemsA list or collection must be queried
HashingKey to location, and fingerprintsFast lookup by key, or detecting change
GraphsThings and their linksRoutes, networks, dependencies
Recursion and dynamic programmingSmall cases reusedSubproblems repeat
Greedy, divide and conquer, backtrackingThree strategiesDesigning a new method
Trees and heapsOrdered or prioritised structuresRanges, hierarchies, most-urgent-first
Ranking and recommendationsScores and similarityOrdering results for a person
Machine learningLearning from examplesRules are too hard to write
EncryptionKeys, signatures, hashesData must be kept private or verified

What experts add

  • Measure firstTime the real program before changing it
  • Clarity firstCode people can read is code people can fix
  • Use librariesThey are tested and tuned by many people
  • Data size decidesSmall inputs hide poor choices
Habits that matter more than any single algorithm.
Expert viewMeasure, profile, and the cost of complexity

Big-O describes how work grows, not how fast a program runs today. A simple O(n squared) method can beat a cleverer O(n log n) one on small inputs because of lower overhead, and memory, caches and input shape all matter. The standard advice is to profile a realistic workload, find the actual hot spot, change it, and measure again. Every clever structure also has a cost: more code, more bugs and more to explain. Famously, premature optimisation, tuning code before you know where time is spent, wastes effort and often damages clarity for no gain.

Myth

The fastest algorithm on paper is always best

Fact

Overheads and small inputs mean a simple method can win. Measure real data

Myth

Writing my own version shows skill and is safer

Fact

Tested library code is usually faster and safer, especially for sorting and security

Two common beliefs.
size of the job (n)work
The reason size decides: how work grows as the list grows.
  • 1 (constant)
  • log n
  • n
  • n log n
  • n squared
  • 2 to the n
Expert viewReading the growth chart

Curves that stay flat or climb gently (constant, log n, n) cope with huge inputs. Curves that bend upward fast (n squared and worse) look harmless at ten items and become impossible at a million. Choosing well often means moving a problem from a steep curve to a gentle one, for example by sorting once and then searching many times, or by using a hash table in place of repeated scans.

Try it by hand: pick the algorithm

Read each scenario, name the question it asks, and choose a first approach. Then check the notes below.

  1. A. 12 names, find oneUnsorted, searched once
  2. B. Map of 8 townsFind the route with fewest road changes
  3. C. A million customer idsLook up a customer by id, again and again
  4. Check your answersCompare with the notes below
Three small scenarios.
  • A: Linear search. Sorting twelve names to look up one costs more than just scanning them.
  • B: Breadth-first search, because it finds the fewest steps first. If roads had different costs and the cheapest total mattered, Dijkstra would be the next step up.
  • C: A hash table keyed by customer id. If you also needed customers in id order, a balanced tree or a sorted array would serve instead.

Name the question, measure the data, and choose the simplest tool that fits.

Frequently asked questions

How do I decide between two algorithms that both fit?

Take the simpler one, and measure on realistic data if speed matters.

Should I always use the lowest Big-O?

No. Big-O describes growth, and small inputs or large overheads can reverse the order.

When should I write my own algorithm?

When a library does not cover your need, or for learning. For sorting and cryptography, use the library.

Is there one algorithm to remember?

No. Remember the questions: find, order, relate, choose, rank, learn, protect.

Back to all articles