Introduction
Imagine you have two ways to find a book in a huge library. One way takes a few seconds, the other takes hours. Understanding why one method is faster helps us solve problems quickly and save time.
Jump into concepts and practice - no test required
Imagine looking for a friend's house in a city. One way is to check every street one by one, which takes hours. Another way is to use a map that shows the exact location, so you get there quickly. The map method is like a fast algorithm.
┌───────────────┐ ┌───────────────┐
│ Problem │──────▶│ Algorithm A │
│ Size (N) │ │ (Fast) │
└───────────────┘ └───────────────┘
│
▼
┌───────────────┐
│ Steps grow │
│ slowly with N │
└───────────────┘
┌───────────────┐ ┌───────────────┐
│ Problem │──────▶│ Algorithm B │
│ Size (N) │ │ (Slow) │
└───────────────┘ └───────────────┘
│
▼
┌───────────────┐
│ Steps grow │
│ quickly with N│
└───────────────┘What does algorithm efficiency mainly measure?
Which of these is a sign of a faster algorithm?
for i in range(n):
print(i)What is the output speed difference between these two algorithms when n is very large?
Algorithm 1: Check every item one by one
Algorithm 2: Jump to the middle, then half repeatedlyFind the error in this slow algorithm and suggest a faster approach:
def find_item(lst, target):
for item in lst:
if item == target:
return True
return FalseYou have a list of 1,000,000 numbers sorted in order. You want to find if the number 500,000 is in the list. Which algorithm is best and why?