A linear algorithm is like a straight line, it grows at a steady pace as you add more things to work with.
Imagine you're lining up your toys to clean them. If you have 5 toys, you pick each one up and put it away, that’s 5 steps. If you get 10 toys, you do 10 steps. No matter how many toys you have, you just add one more step for each new toy. That's what a linear algorithm does, it takes as many steps as there are items to process.
Like a Line of People Waiting
Think about a line at the ice cream shop. If 3 people join the line, they wait for 3 turns. If 10 people come, they wait for 10 turns. Each person adds one more step in the line, just like how a linear algorithm works with data.
No Hops or Jumps
Some algorithms are faster, they can jump ahead or do things all at once. But linear ones are simple: they go one by one, steady and sure, no matter how long the line gets. A linear algorithm is like a straight line, it grows at a steady pace as you add more things to work with.
Imagine you're lining up your toys to clean them. If you have 5 toys, you pick each one up and put it away, that’s 5 steps. If you get 10 toys, you do 10 steps. No matter how many toys you have, you just add one more step for each new toy. That's what a linear algorithm does, it takes as many steps as there are items to process.
Examples
- Counting the number of apples in a basket one by one
- Adding up your weekly allowance
Ask a question
See also
- What is O(log n)?
- What are dynamic data structures?
- How Does Computer Science Basics: Algorithms Work?
- How Does Intro to Algorithms: Crash Course Computer Science #13 Work?
- What are lock-free and wait-free algorithms?