When I wrote my first program, the computer felt like magic. I typed a few lines, and it sorted a list of a hundred names in seconds. How did it know what to do? A senior student laughed and said, “The computer knows nothing — you gave it a recipe.” That recipe has a name: an algorithm. And here is the honest truth nobody told me early on — programming is not really about memorising code. It is about writing clear recipes. Once you can think in steps, the code almost writes itself. Let’s understand algorithms step by step, with a real example.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

What Is an Algorithm, Really?

An algorithm is a clear, step-by-step set of instructions for solving a problem — with a definite start, a definite end, and no guessing in between. A recipe for biryani is an algorithm: wash the rice, soak it, fry the onions, layer everything, cook on low heat. Google Maps finding your route is an algorithm. YouTube deciding which video to suggest next is an algorithm. In code, the ingredients are inputs (what you start with) and the finished dish is the output (what you get). In my opinion, this is the one idea that separates people who "know a programming language" from people who can actually solve problems — and it has almost nothing to do with syntax.

The Main Idea

Most beginners get stuck right here: they jump straight to typing code before they know the steps. That is like starting to cook without reading the recipe — you will burn something. The real skill is thinking first. For any single problem, there are usually many possible algorithms, and some are dramatically better than others. Sorting ten names by hand takes seconds either way; sorting ten million names with a slow method could take hours, while a smart method finishes in seconds. The steps matter more than the language you write them in.

In Simple Words

Here is how to design any algorithm, in five steps:

  1. Define the problem — say it in one sentence: "Find Ali's phone number in this list."
  2. List the input and output — input: a list of names and numbers; output: Ali's number, or "not found".
  3. Write the steps in plain words — no code yet, just numbered steps anyone could follow.
  4. Test it on a small example — run the steps by hand on five names before you trust it on five thousand.
  5. Improve it — count the steps. Can you do the same job in fewer?

Let's Solve an Example: Finding Ali's Number

You have a phonebook with 100 names, sorted A to Z. You need Ali's number. There are two famous ways to find it — let's race them:

StepMethod 1: Linear search (check one by one)Method 2: Binary search (halve the list)
1Check name 1 — not AliOpen the middle (name 50) — "M" — Ali is before M
2Check name 2 — not AliMiddle of the first half (name 25) — "G" — Ali is after G
3Check name 3 — not Ali...Middle of names 25-50 (name 37) — "K" — Ali is before K
4...keep going...A few more halvings...
ResultFound at name 6 — 6 checks (lucky!)Found at name 6 — 6 checks too
Worst case100 checks (Ali could be last)Only 7 checks (100 halves to 1 in 7 steps)

This time both methods tied, because Ali was near the start. But look at the worst case — that is where algorithms earn their money. Linear search might need all 100 checks; binary search never needs more than 7, because every step throws away half the list. Now imagine a million names: linear search could take a million checks, binary search about 20. That is not a small difference — it is the difference between an app that feels instant and one that feels broken. I recommend learning binary search as your first "real" algorithm: it is short, it is beautiful, and it teaches you the most important lesson in computer science — a smarter recipe beats a faster computer. Here is the whole idea in pseudocode:

low = 0, high = 99
while low <= high:
    mid = (low + high) / 2
    if names[mid] == "Ali": found!
    elif names[mid] < "Ali": low = mid + 1
    else: high = mid - 1
Diagram: binary search finding Ali's name in a 100-name phonebook by halving the list step by step, from 100 names down to 1

Animated: binary search halving the phonebook step by step - 100 names, then 50, then 25, then Ali found

Linear Search vs Binary Search

Linear searchBinary search
How it worksCheck every item, one by oneHalve the list again and again
Needs sorted data?No — works on any listYes — the list must be sorted first
Worst case (100 items)100 checks7 checks
Worst case (1 million items)1,000,000 checksAbout 20 checks
Best forSmall or unsorted listsBig, sorted lists

Binary search is faster, but it has one condition: the list must be sorted. Sorting first costs time too, so the honest answer is "it depends" — and knowing when each method wins is exactly what thinking in algorithms is about. If you want to see sorting itself in action, my selection sort guide walks through a sorting recipe step by step, and my bubble sort guide shows the simplest sorting method of all.

3 Mistakes Every Beginner Makes

I made all three, so you do not have to. First, skipping the plan and typing code straight away. Ten minutes with a pencil saves an hour of debugging — write the steps in plain words first, always. Second, testing only the easy case. Your algorithm works on five names? Great. Now test it on an empty list, a list with one name, and a list where the answer is not there at all — that is where real programs break. Third, falling in love with your first idea. The first recipe that works is rarely the best one; ask yourself "can I do this in fewer steps?" at least once. Once you start counting steps, you are ready for the next level — my Big O notation guide shows how programmers measure an algorithm's speed with one simple idea.

Test Your Understanding

Quiz: You have a sorted list of 1,000 names and you use binary search. Roughly how many checks do you need in the worst case?

  1. 1,000 checks
  2. About 10 checks
  3. 500 checks
  4. Just 1 check

Answer: B. Halving 1,000 ten times leaves about 1 item (2 to the power of 10 is 1,024) — so around 10 checks, not 1,000.

Conclusion

An algorithm is simply a clear recipe for solving a problem: define it, list your input and output, write the steps, test them small, then improve. Ali's phonebook shows why the recipe matters more than the machine — binary search finds a name in a million in about 20 checks, while checking one by one could take a million. So try this today: pick a tiny daily problem — sorting your books, finding the cheapest item in a list — and write its steps on paper before you touch a keyboard. That habit is the real beginning of programming.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Written by Muhammad Junaid Niazi

Founder of The Geeks Guru — writing simple, practical tech and programming tutorials from Layyah, Pakistan.

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading