As a software engineering intern, one of the first things that surprised me was how often "algorithms" come up,not just in interviews, but in everyday coding tasks like sorting data, searching lists, or optimizing features. In this post, I'll explain what an algorithm really is, cover the main different types/categories with simple examples, and share some of the best visual videos (with animations) that helped me understand them better.
In this post (my first one here), I'll explain:
- What searching algorithms do (without the yawn)
- What sorting algorithms do (because chaos is fun, but order is faster)
- What divide and conquer is.
- Simple examples + how fast they are (Big O explained)
- Best animated videos to watch so you see everything moving (because who reads when you can watch bars dance?)
Let's dive in step by step, slow and clear.
1. What Are Searching Algorithms? (Finding Stuff in a List)
Searching -> "Look for one item in a big list (array) and say where it is or if it's not there."
Two main easy ones:
Linear Search
- How: Check from first item to last, one by one.
- Example: Finding your phone in a messy bag , check every pocket until you find it.
- Good: Very easy code, works on any list (sorted or not).
- Bad: Slow on big lists (worst case check everything ,imagine searching a haystack for a needle, literally).
- Speed (Big O): O(n) – time grows same as list size n. 1 million items? Might check 1 million times! (Your computer will sigh.) Quick code example (JavaScript)
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // found at position i
}
}
return -1; // not found
}
const numbers = [4, 2, 7, 1, 9, 5];
console.log(linearSearch(numbers, 7)); // Output: 2
How the array looks while searching for 7:
textStep 0: [4, 2, 7, 1, 9, 5] ← check 4? No
Step 1: [4, 2, 7, 1, 9, 5] ← check 2? No
Step 2: [4, 2, 7, 1, 9, 5] ← check 7? YES! Stop
Binary Search
- How: Only on sorted list! Check middle item. If too small, ignore left half. If too big, ignore right half. Repeat.
- Example: Finding a word in a dictionary – open middle page, decide left or right, repeat. (No more flipping every page)
- Good: Crazy fast! Feels like cheating.
- Bad: List must be sorted first (extra step, like cleaning your room before guests arrive).
- Speed (Big O): O(log n) – very slow growth. 1 million items? Only about 20 checks max! (Your computer high-fives you.)
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
const sorted = [1, 3, 5, 7, 9, 11, 13];
console.log(binarySearch(sorted, 7)); // Output: 3
Visualization of searching for 7 (halving each time):
Initial: [1, 3, 5, 7, 9, 11, 13]
↑ ↑
left right
Mid = 3 → check 7 → Found!
(If looking for 12 → would ignore left half, then right half, etc.)
Only ~3 steps instead of checking everything , magic!
Binary search is much better for big sorted data (like user IDs in a database). Pro tip: Always sort first if you're searching a lot ,save yourself from linear regret.
2. What Are Sorting Algorithms? (Putting Things in Order)
Sorting = "Arrange list from small to big (or A to Z)." Why? So binary search doesn't laugh at you, and your data looks like it has its life together.
We sort to make searching faster, or to display nicely (like high scores nobody wants the winner at the bottom).
Common easy ones:
Bubble Sort
- How: Compare two next items. If wrong order, swap. Repeat until no swaps.
- Example: Kids in line by height – keep swapping if taller is in front. (Imagine the chaos if they were bubbles popping up ,hence the name.)
- Good: Super simple to code.
- Bad: Very slow on big lists. It's like traffic in Colombo during rush hour,endless stops and starts.
- Speed: O(n²) – bad for large n. (Your app might time out and go get tea.)
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// swap
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
let nums = [5, 3, 8, 4, 2];
bubbleSort(nums);
console.log(nums); // [2, 3, 4, 5, 8]
How the array "bubbles" after first pass (biggest goes to end):
Start: [5, 3, 8, 4, 2]
After pass 1: [3, 5, 4, 2, 8] ← 8 bubbled to the end
After pass 2: [3, 4, 2, 5, 8]
After pass 3: [3, 2, 4, 5, 8]
... eventually: [2, 3, 4, 5, 8]
Insertion Sort
- How: Start with first item (sorted). Take next, insert in right spot by shifting others.
- Example: Sorting playing cards in hand – slide each new card where it belongs. (Feels like organizing your closet... one sock at a time.)
- Good: Fast on small or almost-sorted lists.
- Speed: O(n²) worst, but O(n) if nearly sorted. (Like a lazy Sunday clean-up.)
Quick Sort (very common)
- How: Pick one item (pivot). Put smaller left, bigger right. Then sort left and right same way.
- Example: Divide friends into shorter/taller than you, repeat in each group. (If you pick a bad pivot, it's like choosing the wrong team captain chaos ensues.)
- Good: Fast in real life.
- Bad: Worst case slow if bad pivot (but rare, like winning the lottery.. backwards).
- Speed: Average O(n log n) – excellent! (Feels quick, hence the name.)
Merge Sort
- How: Cut list in half, sort each half, merge back sorted.
- Example: Split deck of cards, sort halves, combine neatly. (Like merging two lines at a wedding smooth if done right.)
- Good: Always fast, keeps order of equal items.
- Bad: Needs extra space (like needing a spare room for your mess).
- Speed: O(n log n) always. (Reliable, like that one friend who shows up on time.)
Many fast sorts use divide and conquer (next section!). Without it, we'd be stuck with slow sorts forever yikes.
3. What Is Divide and Conquer? (The Smart Trick Behind Many Fast Algorithms)
Divide and Conquer = Break big problem into smaller similar problems, solve small ones, put answers together. It's the "work smarter, not harder" of algorithms like delegating chores to siblings instead of doing everything yourself.
Three easy steps:
- Divide — Split problem into 2 (or more) smaller parts. (Chop it up!)
- Conquer — Solve each small part (often by calling same method again = recursion). (Attack the mini-bosses.)
- Combine — Join solutions to get full answer. (Put the puzzle back together.)
Why it's great: Small problems are easy. Doing many small ones is faster than one huge one. (Because tackling a mountain one rock at a time is better than staring at it.)
Real examples we already saw:
-
Binary Search is divide and conquer!
- Divide: Cut list in half (ignore one half).
- Conquer: Search the half (repeat).
- Combine: Nothing – just return position if found. (Sneaky simple.)
-
Merge Sort is classic divide and conquer!
- Divide: Split list into two halves.
- Conquer: Sort each half (repeat until tiny lists of 1 item – already sorted).
- Combine: Merge two sorted halves into one big sorted list. (The "combine" is where the magic happens.)
-
Quick Sort is divide and conquer too!
- Divide: Partition around pivot (smaller left, bigger right).
- Conquer: Sort left and right parts.
- Combine: Nothing – parts are already in place. (Quick and cheeky.)
Visualization of divide and conquer in Merge Sort
Original array: [38, 27, 43, 3, 9, 82, 10]
Step 1 – Divide:
[38, 27, 43, 3] [9, 82, 10]
Step 2 – Divide more:
[38, 27] [43, 3] [9] [82, 10]
Step 3 – Conquer (sort tiny parts):
[27, 38] [3, 43] [9] [10, 82]
Step 4 – Combine (merge sorted halves):
[3, 27, 38, 43] [9, 10, 82]
Final merge: [3, 9, 10, 27, 38, 43, 82]
Beautiful tree-like splitting and merging , that's the power of divide and conquer!
Quick Sort is divide and conquer too!
Divide: Partition around pivot (smaller left, bigger right).
Conquer: Sort left and right parts.
Combine: Nothing – parts are already in place. (Quick and cheeky.)
Other examples: Finding closest points on map (divide the map!), fast multiplication of big numbers .
Speed summary (Big O simple):
- O(1): instant (like microwave popcorn)
- O(log n): super fast (binary search—blink and it's done)
- O(n): okay (linear search steady but boring)
- O(n log n): good for big lists (merge/quick sort party time)
- O(n²): slow on big data (bubble/insertion/selection snail race)
Divide and conquer gives us O(n log n) sorts instead of slow O(n²)! Without it, algorithms would be as exciting as watching paint dry.
Best Visual Videos to Watch (Animated & Easy to Understand)
Animations show the "divide, conquer, combine" moving – way better than my words. (Plus, they're fun,like algorithm cartoons.)
-
Searching (Linear vs Binary)
- Linear vs Binary Search Animation — Short and clear difference. (Watch binary zoom—mind blown.)
-
Sorting Animations
- 15 Sorting Algorithms in 6 Minutes — Bubble, quick, merge dancing with sounds! (The bubble sort looks hilariously inefficient.)
- Visualizing Sorting Algorithms — Slow explanation + visuals. (Perfect for pausing and laughing at the swaps.)
-
Divide and Conquer Focus
- Divide & Conquer Algorithm In 3 Minutes — Super short intro with merge sort example. (Quick like the name—pun intended.)
- Master Divide & Conquer | Merge Sort, Quick Sort & Binary Search — Visuals of divide/conquer/combine steps. (See the recursion without the headache.)
- Algorithms and Data Structures Tutorial – Full Course (freeCodeCamp) — Searching, sorting, divide and conquer parts. (Long but worth it—like a movie marathon.)
Play Yourself
VisuAlgo.net — Click to see binary search, merge sort, quick sort step by step. (Interactive fun—feel like a algorithm DJ.)
Final Thoughts
Searching finds things, sorting organizes them, divide and conquer makes them fast by breaking big jobs into small ones. (And saves you from code that runs slower than a turtle in flip-flops.)

Top comments (0)