CS Class Lecture 1
CS Class Lecture 1
Your Name
This document is designed for students around Class 8 who are new to the concepts of
algorithmic complexity.
Contents
1 Introduction 1
1.1 Why Should You Care? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Time Complexity 2
2.1 What is Time Complexity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Big-O Notation (Simplified) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Common Time Complexity Examples . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Space Complexity 3
3.1 What is Space Complexity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Example (Array Copy) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
6 Practical Tips 4
7 Conclusion 4
1 Introduction
When we write computer programs, we want to know:
These two ideas help us decide if our solution to a problem is efficient and can be used in
real-world applications or programming contests.
• Understand how your program’s performance changes as you increase the input size.
1
• Write code that can handle larger problems (like sorting thousands or millions of numbers).
• Prepare for competitions where you must solve problems within strict time and memory
limits.
2 Time Complexity
2.1 What is Time Complexity?
Time Complexity is a way to measure how the running time of your program grows as the size
of the input increases. We usually call the input size n. For example:
We want to know how many steps or operations our program does in the worst case (most
difficult scenario).
• O(1): Constant time (instant, like getting the 5th item in an array).
At this level (Class 8), knowing these few examples is enough to start. If you see something
with multiple loops, it might be O(n2 ). If you see a single loop, it might be O(n).
2
3 Space Complexity
3.1 What is Space Complexity?
Space Complexity tells us how much extra memory our program needs, besides the input data.
• If your program only uses a few variables (like counters), that is O(1) space (constant
space).
• If your program creates a new array or list of size n, that is O(n) space.
4. Apply Big-O Simplification: We usually focus on the largest factor of growth and
ignore smaller details. For example, if your code runs in 2n + 5 steps, we say O(n).
• Saving Memory: Storing lots of data (like n new things) means your computer has
to use more RAM. In some contests, you have a memory limit, and using too much can
disqualify your solution.
3
6 Practical Tips
• Start Small: Try to write a simple program and see how it grows with bigger and bigger
inputs.
• Estimate Before Coding: Ask yourself, “Is this program going to take too long or use
too much memory if n is large?”
• Use Examples:
• Learn from Known Algorithms: Sorting, searching, and other common tasks already
have well-known time and space complexities.
7 Conclusion
Time and Space Complexity help us see if our programs will be fast enough and use a reasonable
amount of memory. As a Class 8 student:
• Focus on identifying loops, nested loops, and any extra arrays or lists.
Understanding these basics will help you write better, more efficient programs now and in
the future, especially if you continue with more advanced computer science and programming
courses.
Further Reading/Watching:
• Basic videos on “Time Complexity” on YouTube (search for “Big O for Beginners”).